MethodInvoker vs Control for Control.BeginInvoke [英] MethodInvoker vs Action for Control.BeginInvoke
问题描述
哪个更正确,为什么?
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表达式?
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的实例,在这种情况下,发件人
参数将包含此控件
和event参数将包含
EventArgs.Empty。代理也可以
是MethodInvoker的一个实例,或
任何其他委托,它使用一个void
参数列表。调用
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.
这篇关于MethodInvoker vs Control for Control.BeginInvoke的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!