views:

126

answers:

2

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? Does 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.

+2  A: 

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.

SLaks
So does invoking my control automatically invoke my main form? If I invoke one control on the main form, will other controls on that same form also be invoked?
George
What do you mean? Calling `Invoke` will execute a method on the UI thread. It has nothing to do with a specific control.
SLaks
I gotcha. Thanks!
George
+2  A: 

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.

CodeSavvyGeek