MethodInvoker VS的Control.BeginInvoke行动 [英] MethodInvoker vs Action for Control.BeginInvoke

查看:288
本文介绍了MethodInvoker VS的Control.BeginInvoke行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是比较正确的,为什么?

Which is more correct and why?

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 动作,甚至写一个lambda EX pression?

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 VS 动作,而 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:

该委托可以是一个实例   事件处理程序,在这种情况下,发送者   参数将包含这种控制,   和事件参数将包含   EventArgs.Empty。该代表还可以   是MethodInvoker的实例,或   任何其他代表,需要一个空白   参数列表。拨打到   事件处理程序或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.BeginInvoke行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆