哪个更正确,为什么?
Control.BeginInvoke(new Action(DoSomething), null);
private void DoSomething()
{
MessageBox.Show("What a great post");
}
或
Control.BeginInvoke((MethodInvoker) delegate {
MessageBox.Show("What a great post");
});
我有点觉得我在做同样的事情,那么什么时候使用 MethodInvoker
vs Action
,甚至写一个 lambda 表达式?
编辑:我知道编写 lambda 与 Action
之间并没有太大区别,但是 MethodInvoker
似乎是为特定目的而制作。它有什么不同吗?
最佳答案
两者都同样正确,但是 Control.Invoke
的文档指出:
The delegate can be an instance of EventHandler, in which case the sender parameter will contain this control, and the event parameter will contain EventArgs.Empty. The delegate can also be an instance of MethodInvoker, or any other delegate that takes a void parameter list. A call to an EventHandler or MethodInvoker delegate will be faster than a call to another type of delegate.
所以 MethodInvoker
会是一个更高效的选择。
关于c# - MethodInvoker 与 Control.BeginInvoke 的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1167771/