问题描述
哪个更正确,为什么?
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
与 Action
比较合适,甚至是写一个 lambda 表达式?
I kinda feel like I am doing the same thing, so when is the right time to use MethodInvoker
vs Action
, or even writing a lambda expression?
我知道编写 lambda 与 Action
之间并没有太大区别,但 MethodInvoker
似乎是为特定目的而制作.它有什么不同吗?
I know that there isn't really much of a difference between writing a lambda vs Action
, but MethodInvoker
seems to be made for a specific purpose. Is it doing anything different?
推荐答案
两者都同样正确,但 Control.Invoke
声明:
Both are equally correct, but the documentation for Control.Invoke
states that:
委托可以是EventHandler,在这种情况下是发送者参数将包含此控件,并且事件参数将包含EventArgs.Empty.委托人也可以是 MethodInvoker 的一个实例,或者任何其他无效的代表参数列表.一个电话EventHandler 或 MethodInvoker 委托会比打电话给另一个人更快代表的类型.
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
会是更有效的选择.
So MethodInvoker
would be a more efficient choice.
这篇关于Control.BeginInvoke 的 MethodInvoker 与 Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!