Monday, June 15, 2009

Threading || Control.Invoke



In a multi-threaded Windows Forms application, it's illegal to call a method or property on a control from any thread other than the one that created it.

All cross-thread calls must be explicitly marshalled to the thread that created the control (usually the main thread), using the Control.Invoke or Control.BeginInvoke method.

One cannot rely on automatic marshalling because it takes place too late – only when execution gets well into unmanaged code, by which time plenty of internal .NET code may already have run on the "wrong" thread – code which is not thread-safe.



WPF is similar to Windows Forms in that elements can be accessed only from the thread that originally created them. The equivalent to Control.Invoke in WPF is Dispatcher.Invoke.


Hope this helps.


Thanks & Regards,

Arun Manglick || Senior Tech Lead



No comments:

Post a Comment