MethodInvoker vs Action for Control.BeginInvoke

后端 未结 7 940
走了就别回头了
走了就别回头了 2020-11-28 22:33

Which is more correct and why?

Control.BeginInvoke(new Action(DoSomething), null);

private void DoSomething()
{
    MessageBox.Show(\"What a great post\");
         


        
7条回答
  •  不知归路
    2020-11-28 22:59

    Also per MSDN:

    MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. This delegate can be used when making calls to a control's Invoke method, or when you need a simple delegate but do not want to define one yourself.

    an Action on the other hand can take up to 4 parameters.

    But I don't think there is any difference between MethodInvoker and Action as they both simply encapsulate a delegate that doesn't take a paremter and returns void

    If you look at their definitions you'll simply see this.

    public delegate void MethodInvoker();
    public delegate void Action();
    

    btw you could also write your second line as.

    Control.BeginInvoke(new MethodInvoker(DoSomething), null);
    

提交回复
热议问题