匿名方法作为BeginInvoke的参数?

时间:2022-08-27 21:25:25

Why can't you pass an anonymous method as a parameter to the BeginInvoke method? I have the following code:

为什么不能将匿名方法作为参数传递给BeginInvoke方法?我有以下代码:

private delegate void CfgMnMnuDlg(DIServer svr);
private void ConfigureMainMenu(DIServer server,)
{
    MenuStrip mnMnu = PresenterView.MainMenu;
    if (mnMnu.InvokeRequired)
    {
        mnMnu.BeginInvoke((CfgMnMnuDlg)ConfigureMainMenu, 
                            new object[] { server});
    }
    else
    {
        // Do actual work here
    }
}

I'm trying to avoid declaring the delegate. Why can't I write something like the below instead? Or can I, and I just can't figure out the correct syntax? The below currently generates an:

我试图避免宣布代表。为什么我不能写下面的东西呢?或者我可以,我只是无法弄清楚正确的语法?以下目前生成:

Argument type 'Anonymous method' is not assignable to parameter type 'System.Delegate'

参数类型'System.Delegate'不能赋予参数类型'匿名方法'

Ok, that's right of course, but is there some other syntax I can use to do this (avoid having to declare a separate delegate in order to use BeginInvoke()?

好吧,这当然是正确的,但我是否可以使用其他一些语法来做到这一点(避免为了使用BeginInvoke()而声明一个单独的委托?

(Being able to do this would fit in neatly with the concept of using anon methods/lamdas in place of explicit delegates which works so cleanly everywhere else.)

(能够做到这一点将完全符合使用anon方法/ lamdas代替显式委托的概念,这些委托在其他地方干净利落地工作。)

private void ConfigureMainMenu(DIServer server,)
{
    MenuStrip mnMnu = PresenterView.MainMenu;
    if (mnMnu.InvokeRequired)
    {
        mnMnu.BeginInvoke(  //  pass anonymous method instead ?
             delegate(DIServer svr) { ConfigureMainMenu(server);},     
             new object[] { server});
    }
    else
    {
        // Do actual work here
    }
}

6 个解决方案

#1


Try this:

control.BeginInvoke((MethodInvoker) delegate { /* method details */ });

Or:

private void ConfigureMainMenu(DIServer server)
{
    if (control.InvokeRequired)
    {
        control.BeginInvoke(new Action<DIServer >(ConfigureMainMenu), server);
    }
    else
    {
        /* do work */
    }
}

Or:

private void ConfigureMainMenu(DIServer server)
{
    MenuStrip mnMnu = PresenterView.MainMenu;
    if (mnMnu.InvokeRequired)
    {
        // Private variable
        _methodInvoker = new MethodInvoker((Action)(() => ConfigureMainMenu(server)));
        _methodInvoker.BeginInvoke(new AsyncCallback(ProcessEnded), null); // Call _methodInvoker.EndInvoke in ProcessEnded
    }
    else
    {
        /* do work */
    }
}

#2


You should be able to write something like this:

你应该能写出这样的东西:

private void ConfigureMainMenu(DIServer server,)
{
    MenuStrip mnMnu = PresenterView.MainMenu;
    if (mnMnu.InvokeRequired)
    {
        mnMnu.BeginInvoke(new Action<DIServer>(ConfigureMainMenu), 
                            new object[] { server});
    }
    else
    {
        // Do actual work here
    }
}

#3


You could write an extension method that would wrap anonymous methods, and even take care of the InvokeRequired semantics:

您可以编写一个扩展方法来包装匿名方法,甚至可以处理InvokeRequired语义:

public static void InvokeAction(this Control ctl, Action a)
{
    if (!ctl.InvokeRequired)
    {
        a();
    }
    else
    {
        ctl.BeginInvoke(new MethodInvoker(a));
    }
}

This would allow you to do:

这将允许您这样做:

control.InvokeAction(delegate() { ConfigureMainMenu(server); });

#4


You can do this in a single method by calling invoking yourself:

您可以通过调用自己调用在单个方法中执行此操作:

  ClassData updData =  new ClassData();

  this.BeginInvoke(new Action<ClassData>(FillCurve),
                           new object[] { updData });

...

public void FillCurve(ClassData updData)
{
 ...
}

#5


For completely anonymous methods with a limited number of parameters:

对于具有有限数量参数的完全匿名方法:

Func<int, int?> caller = new Func<int, int?>((int param1) =>
   {
      return null;
   });

caller.BeginInvoke(7, new AsyncCallback((IAsyncResult ar) =>
{
   AsyncResult result = (AsyncResult)ar;
   Func<int, int?> action = (Func<int, int?>)result.AsyncDelegate;
   action.EndInvoke(ar);
}), null);

You can use one of the other Func delegate types as needed.

您可以根据需要使用其他Func委托类型之一。

#6


I've tried a bunch of different methods but none work. ie...

我尝试过一些不同的方法但没有工作。即...


// Fails -- cannot convert lamda to System.Delegate
mnMnu.BeginInvoke( (DIServer svr)=> {ConfigureMainMenu(server);}, new object[] server);
// Fails -- cannot convert anonymous method to System.Delegate
mnMnu.BeginInvoke( new delegate(DIServer svr){ConfigureMainMenu(server);}, new object[] server);

So, the short answer is no. You could create short helper delegates in the given context and use lambdas to make it a bit neater but that's pretty much it.

所以,简短的回答是否定的。您可以在给定的上下文中创建简短的助手委托,并使用lambdas使其更整洁,但这就是它。

EDIT: Turns out I'm wrong. The methodinvoker answer below works. See this page

编辑:结果我错了。下面的methodinvoker答案有效。见本页

#1


Try this:

control.BeginInvoke((MethodInvoker) delegate { /* method details */ });

Or:

private void ConfigureMainMenu(DIServer server)
{
    if (control.InvokeRequired)
    {
        control.BeginInvoke(new Action<DIServer >(ConfigureMainMenu), server);
    }
    else
    {
        /* do work */
    }
}

Or:

private void ConfigureMainMenu(DIServer server)
{
    MenuStrip mnMnu = PresenterView.MainMenu;
    if (mnMnu.InvokeRequired)
    {
        // Private variable
        _methodInvoker = new MethodInvoker((Action)(() => ConfigureMainMenu(server)));
        _methodInvoker.BeginInvoke(new AsyncCallback(ProcessEnded), null); // Call _methodInvoker.EndInvoke in ProcessEnded
    }
    else
    {
        /* do work */
    }
}

#2


You should be able to write something like this:

你应该能写出这样的东西:

private void ConfigureMainMenu(DIServer server,)
{
    MenuStrip mnMnu = PresenterView.MainMenu;
    if (mnMnu.InvokeRequired)
    {
        mnMnu.BeginInvoke(new Action<DIServer>(ConfigureMainMenu), 
                            new object[] { server});
    }
    else
    {
        // Do actual work here
    }
}

#3


You could write an extension method that would wrap anonymous methods, and even take care of the InvokeRequired semantics:

您可以编写一个扩展方法来包装匿名方法,甚至可以处理InvokeRequired语义:

public static void InvokeAction(this Control ctl, Action a)
{
    if (!ctl.InvokeRequired)
    {
        a();
    }
    else
    {
        ctl.BeginInvoke(new MethodInvoker(a));
    }
}

This would allow you to do:

这将允许您这样做:

control.InvokeAction(delegate() { ConfigureMainMenu(server); });

#4


You can do this in a single method by calling invoking yourself:

您可以通过调用自己调用在单个方法中执行此操作:

  ClassData updData =  new ClassData();

  this.BeginInvoke(new Action<ClassData>(FillCurve),
                           new object[] { updData });

...

public void FillCurve(ClassData updData)
{
 ...
}

#5


For completely anonymous methods with a limited number of parameters:

对于具有有限数量参数的完全匿名方法:

Func<int, int?> caller = new Func<int, int?>((int param1) =>
   {
      return null;
   });

caller.BeginInvoke(7, new AsyncCallback((IAsyncResult ar) =>
{
   AsyncResult result = (AsyncResult)ar;
   Func<int, int?> action = (Func<int, int?>)result.AsyncDelegate;
   action.EndInvoke(ar);
}), null);

You can use one of the other Func delegate types as needed.

您可以根据需要使用其他Func委托类型之一。

#6


I've tried a bunch of different methods but none work. ie...

我尝试过一些不同的方法但没有工作。即...


// Fails -- cannot convert lamda to System.Delegate
mnMnu.BeginInvoke( (DIServer svr)=> {ConfigureMainMenu(server);}, new object[] server);
// Fails -- cannot convert anonymous method to System.Delegate
mnMnu.BeginInvoke( new delegate(DIServer svr){ConfigureMainMenu(server);}, new object[] server);

So, the short answer is no. You could create short helper delegates in the given context and use lambdas to make it a bit neater but that's pretty much it.

所以,简短的回答是否定的。您可以在给定的上下文中创建简短的助手委托,并使用lambdas使其更整洁,但这就是它。

EDIT: Turns out I'm wrong. The methodinvoker answer below works. See this page

编辑:结果我错了。下面的methodinvoker答案有效。见本页