Invoke(Delegate)

后端 未结 9 1138
误落风尘
误落风尘 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 16:43

    In practical terms it means that the delegate is guaranteed to be invoked on the main thread. This is important because in the case of windows controls if you don't update their properties on the main thread then you either don't see the change, or the control raises an exception.

    The pattern is:

    void OnEvent(object sender, EventArgs e)
    {
       if (this.InvokeRequired)
       {
           this.Invoke(() => this.OnEvent(sender, e);
           return;
       }
    
       // do stuff (now you know you are on the main thread)
    }
    

提交回复
热议问题