Re: Updating controls on a form
Phil Sayers <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
kelly, you may want to look ath this series of 3 articles from chris sells. it made all the multithreading stuff very clear to me, and helped me figure out the separate pieces i needed to move information back and forth between threads, how to safely handle events from one thread and make sure i correctly manipulated my UI controls on the proper thread. article #1 should be all you need here. #2 and #3 are more advanced scenarios i think. [1]http://msdn2.microsoft.com/en-us/library/ms951089.aspx [2]http://msdn2.microsoft.com/en-us/library/ms951109.aspx [3]http://msdn2.microsoft.com/en-us/library/ms993020.aspx -----Original Message----- From: Discussion forum for developers using Windows Forms to build apps and controls [mailto:[email protected]]On Behalf Of Kelly Baker Sent: Sunday, March 25, 2007 7:18 PM To: [email protected] Subject: Re: [DOTNET-WINFORMS] Updating controls on a form There is no form in my dll. The Windows form is in the calling executable. I do want the timer and the code that it runs in a separate thread. I was just wondering how I could have it doing the grunt work in a separate thread, but have shared variables that the properties return, without the calling executable having to go through all of this. Thanks, Kelly -----Original Message----- From: Discussion forum for developers using Windows Forms to build apps and controls [mailto:[email protected]] On Behalf Of Patrick Steele Sent: Sunday, March 25, 2007 7:28 AM To: [email protected] Subject: Re: [DOTNET-WINFORMS] Updating controls on a form You should be using the System.Windows.Forms.Timer instead. From the documentation on System.Threading.Timer: "System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms." -- Patrick Steele http://weblogs.asp.net/psteele -----Original Message----- From: Discussion forum for developers using Windows Forms to build apps and controls [mailto:[email protected]] On Behalf Of Kelly Baker Sent: Saturday, March 24, 2007 10:37 PM To: [email protected] Subject: [DOTNET-WINFORMS] Updating controls on a form Unless my app is EXTREMELY simple, I get the cross-threading error message regularly when updating controls on a Windows form. My latest example involves a dll that runs a System.Threading.Timer, updating private shared variables. When it finishes updating, it raises a NewData event. My app responds to that event by updating display controls (labels in this case). I have to use a delegate and update them like this: Private Sub SetText(ByVal [text] as String) If Me.Messages.InvokeRequired Then Dim d as New SetTextCallBack(AddressOf SetText) Me.Invoke(d, New Object() {[text]}) Else Me.Messages.Text = [text] End If End Sub I have to write a different routine like this for each control on the form that I want to update. Is there a better way? Please?