Invoke(Delegate)

后端 未结 9 1114
误落风尘
误落风尘 2020-11-22 16:33

Can anybody please explain this statement written on this link

Invoke(Delegate):

Executes the specified delegate on the thread that owns th

9条回答
  •  迷失自我
    2020-11-22 17:01

    A control or window object in Windows Forms is just a wrapper around a Win32 window identified by a handle (sometimes called HWND). Most things you do with the control will eventually result in a Win32 API call that uses this handle. The handle is owned by the thread that created it (typically the main thread), and shouldn't be manipulated by another thread. If for some reason you need to do something with the control from another thread, you can use Invoke to ask the main thread to do it on your behalf.

    For instance, if you want to change the text of a label from a worker thread, you can do something like this:

    theLabel.Invoke(new Action(() => theLabel.Text = "hello world from worker thread!"));
    

提交回复
热议问题