gpt4 book ai didi

c# - MethodInvoker 与 Control.BeginInvoke 的操作

转载 作者:IT王子 更新时间:2023-10-29 03:46:48 32 4
gpt4 key购买 nike

哪个更正确,为什么?

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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com