views:

57

answers:

4

I'd like to know if the executing thread is the one that created a given control instance (to prevent cross-thread if it's not). Is that possible ?

+2  A: 

It sounds like you are looking for Control.InvokeRequired

Chris Taylor
That's it ! Thank you.
Julien
+1  A: 

Check for control.InvokeRequired flag. if it is true, use the control.Invoke function to perform the operation.

Here is an article which looks in to the different scenarios in detail: http://weblogs.asp.net/justin_rogers/articles/126345.aspx

NimsDotNet
Thank you too :-)
Julien
A: 

It is a good idea also to set Control::CheckForIllegalCrossThreadCalls to true in the beginning of the program, to get predictable crash in the case of incorrect cross-thread call.

Alex Farber
A: 

A great article (in my opinion) is the one available at http://www.albahari.com/threading/. It goes over all that you need to know about threading in C#, from Mutex to lock to Control.Invoke. I myself came from the Java world, and thought that C# threads and synchronization was the same, but this web page set me straight. :-)

Patrick