1

I assume a mutex is locked when the passed function being executed, but not very sure. Another question is, say label1 is a member of form1. My experiments showed that Invoke using form1or label1to update the Text property of label1 both work. But what is the difference?

Afterbunny
  • 71
  • 1
  • 6
  • 2
    [Control FindMarshalingControl()](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,6258) `=>` [MarshaledInvoke](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,7734) `=>` [GetWindowThreadProcessId : Same thread?](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,7774) `=>` [ExecutionContext.Capture()](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,7783) `=>` follow. – Jimi Apr 14 '19 at 14:59

2 Answers2

1

The Control.Invoke() method sends invocation of the passed method to the control's thread message loop and returns after the invocation finishes (after the method gets called and the context is switched back to your thread).

The Control.BeginInvoke() method just posts the invocation to the control's thread message queue and continues immediately (does not wait until the passed method executes).

You can use both methods for updating the UI from background threads.

It does not matter which control (or form) you use for calling Invoke or BeginInvoke.

Jiri Volejnik
  • 1,034
  • 6
  • 9
  • Add an important note: `BeginInvoke()` can be safely called from the same thread. It's handled asynchronously. It can be very useful if you want to trigger an Action that requires a *delay* (let other `WM_` messages already in the message queue be processed before the one triggered by the Action is). – Jimi Apr 15 '19 at 09:14
0

Different question but this answer will help you to understand Thread Control.Invoke.

Thread Control.Invoke