Invoking a Thread .NET .this vs. .control
I've noticed that when "giving focus" back to your main thread, when invoking from another thread, you can either invoke "this" or the control that you would like to, well invoke. I've noticed this doesn't matter when giving control back, so can anyone explain why I would invoke a control over the main thread? Doe开发者_Go百科s it matter, or is the main thread being implicitly invoked?
Would,
this.Invoke(InvokedMethod,args)
be different from
button1.Invoke(InvokedMethod,args)
When button1 is on my main form.
Invoking on a control uses the control's handle to determine which thread is the control's UI thread, and then uses that thread to execute. It is possible to have multiple UI threads (a UI thread is any thread running a message loop), or it is possible for a control to have a handle to a non-UI thread (if progamatically creating controls incorrectly). Usually, there is no difference between invoking on the main form or a control, but it may matter in certain circumstances.
The Invoke
methods are defined by the Control
class.
Therefore, they are accessible from any Control
instance.
When you write a form, your class inherits the Form
class, which (indirectly) inherits Control
. Therefore, they're also accessible through the this
instance.
It doesn't matter which one you choose.
精彩评论