c# - MethodInvoker 与 Control.BeginInvoke 的操作

标签 c# .net delegates invoke

哪个更正确,为什么?

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/

相关文章:

c# - 捕捉和重新抛出 .NET 异常的最佳实践

c# - Java 中的 ["column"][rowIndex] 行可以使用什么数据收集?

c# - 使用 libpcap 捕获 "MMM"DateTime 格式时出错

c# - 如何在没有 ef 的情况下在 MVC5 中使用参数进行搜索

iphone - 在 Controller 之间传递数据的 "delegates"的替代方案是什么?

c# - 在字符串中动态插入换行符

c# - 必须处理哪些变量? (.NET/ java )

.net - DotNet 2.0 或 3.5、IE、XSL 和 XSL-FO 功能是什么?

c# - 委托(delegate)和跨线程异常

qt - QML,GridView 委托(delegate) : ReferenceError: `<variable_name>` is not defined