c++handle_method.invoke方法的作用

(5) 2024-07-24 21:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
c++handle_method.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");
});

两者都是正确的,但 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将是一个更有效的选择。

查看MethodInvoker的命名空间可以得知其位Windows.Forms 由此可见更新winforms界面使用MethodInvoker更合适

namespace System.Windows.Forms
{

    //
    // 摘要:
    //     表示一个委托,该委托可执行托管代码中声明为 void 且不接受任何参数的任何方法。
    public delegate void MethodInvoker();
}

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复