MethodInvoker vs Action for Control.BeginInvoke

后端 未结 7 922
走了就别回头了
走了就别回头了 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:51

    Action is defined in System, while MethodInvoker is defined in System.Windows.Forms - you may be better off using Action, since it is portable to other places. You will also find more places that accept Action as a parameter than MethodInvoker.

    However, the documentation does indicate that calls to delegates of type EventHandler or MethodInvoker in Control.Invoke() will be faster than any other type.

    Aside from which namepsace they are in, I don't believe there is a meaningful functional difference between Action and MethodInvoker - they are essentially both defined as:

    public delegate void NoParamMethod();
    

    As an aside, Action has several overloads which allow parameters to be passed in - and it is generic so that they can be typesafe.

提交回复
热议问题