Re: Making threadsafe calls : C# 2005

Eddie Lascu <[email protected]> Tue, 19 Jun 2007 14:32:52 -0400
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
Add this line to the YourForm_Load method for the form where you update
controls whenever the events are raised:

   System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;

If you get weird behaviors then go back to investigate that article,
otherwise you'll be just fine.

-----Original Message-----
From: Discussion relating to the specifics of the C# and Managed C++
languages [mailto:[email protected]]On Behalf Of Peter
Osucha
Sent: Tuesday, June 19, 2007 2:25 PM
To: [email protected]
Subject: [DOTNET-CX] Making threadsafe calls : C# 2005


Hi guys,



I occasionally forget (well more often than not I cheat and ignore the
errors) to make threadsafe calls to form controls.  A good example I am
facing now - I have an object ('Instrument') that is handling messages
from a connected actual hardware instrument by way of a TCP Socket
connection.  This Instrument object raises many events so that a
subscribing form containing an object instance can provide feedback to
the user.



So I have reviewed the 'How to make thread safe calls to Windows Form
Controls' topic in MSDN and I am puzzled by the example (or I just don't
understand it well).  The topic offers a code sample (shown below)...



// This event handler creates a thread that calls a

// Windows Forms control in a thread-safe way.

private void setTextSafeBtn_Click(

    object sender,

    EventArgs e)

{

    this.demoThread =

        new Thread(new ThreadStart(this.ThreadProcSafe));



    this.demoThread.Start();

}



// This method is executed on the worker thread and makes

// a thread-safe call on the TextBox control.

private void ThreadProcSafe()

{

    this.SetText("This text was set safely.");

}



It seems that to implement this type of thread-safe calling will require
a whole lot of code (it seems that I will need such a 'setup' for
modifying each and every form control I want to modify.



Maybe a more concrete example of what I'm talking about would be useful.
My Instrument object raises among other events a 'DataReceived() event'
which the form has a handler for.  When this event is handled by the
form, it updates several controls on the form.  So I can see how I would
create a 'demoThread' in the event handler that calls a separate method
(similar to 'ThreadProcSafe' in the example above) but it seems that I'd
have to call several different 'ThreadProcSafe' methods to handle all
the controls updating I require.  I guess I could have the newly created
friend call a method called UpdateFormControls (that updates every
control on the form whether its needed or not) but that seems like a
not-too-good way to do the updating.



Surely I'm missing something.  Any comments?



Sincerely,

peter






===================================
This list is hosted by DevelopMentor.  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com