调度员。BeginInvoke:无法将lambda转换为System.Delegate

时间:2022-08-27 20:50:49

I'm trying to call System.Windows.Threading.Dispatcher.BeginInvoke. The signature of the method is this:

我正在调用system . windows . thread . dispatcher . begininvoke方法。该方法的签名如下:

BeginInvoke(Delegate method, params object[] args)

I'm trying to pass it a Lambda instead of having to create a Delegate.

我试着给它传递一个Lambda,而不是创建一个委托。

_dispatcher.BeginInvoke((sender) => { DoSomething(); }, new object[] { this } );

It's giving me a compiler error saying that I

它给了我一个编译错误说I

can't convert the lambda to a System.Delegate.

不能将lambda转换为System.Delegate。

The signature of the delegate takes an object as a parameter and returns void. My lambda matches this, yet it's not working. What am I missing?

委托的签名接受一个对象作为参数并返回void。我的lambda与此匹配,但它不工作。我缺少什么?

5 个解决方案

#1


63  

Since the method takes a System.Delegate, you need to give it a specific type of delegate, declared as such. This can be done via a cast or a creation of the specified delegate via new DelegateType as follows:

因为这个方法有一个系统。委托,你需要给它一个特定类型的委托,它是这样声明的。这可以通过cast或通过新delegate类型创建指定的委托来实现,如下所示:

_dispatcher.BeginInvoke(
     new Action<MyClass>((sender) => { DoSomething(); }),
     new object[] { this } 
  );

Also, as SLaks points out, Dispatcher.BeginInvoke takes a params array, so you can just write:

而且,正如SLaks指出的,调度员。BeginInvoke使用一个params数组,所以您可以只写:

_dispatcher.BeginInvoke(
     new Action<MyClass>((sender) => { DoSomething(); }),
     this
  );

Or, if DoSomething is a method on this object itself:

或者,如果DoSomething是这个对象本身的一个方法:

_dispatcher.BeginInvoke(new Action(this.DoSomething));

#2


60  

Shorter:

短:

_dispatcher.BeginInvoke((Action)(() => DoSomething()));

#3


5  

Using Inline Lambda...

使用内联λ……

Dispatcher.BeginInvoke((Action)(()=>{
  //Write Code Here
}));

#4


5  

If you reference System.Windows.Presentation.dll from your project and add using System.Windows.Threading then you can access an extension method that allows you to use the lambda syntax.

如果你引用System.Windows.Presentation。使用System.Windows从项目中添加dll。线程化之后,您可以访问允许使用lambda语法的扩展方法。

using System.Windows.Threading;

...

Dispatcher.BeginInvoke(() =>
{
});

#5


0  

We create extension methods for this. E.g.

我们为此创建扩展方法。如。

public static void BeginInvoke(this Control control, Action action)
    => control.BeginInvoke(action);

Now we can call it from within a form: this.BeginInvoke(() => { ... }).

现在我们可以从表单中调用它:this.BeginInvoke() =>{…})。

#1


63  

Since the method takes a System.Delegate, you need to give it a specific type of delegate, declared as such. This can be done via a cast or a creation of the specified delegate via new DelegateType as follows:

因为这个方法有一个系统。委托,你需要给它一个特定类型的委托,它是这样声明的。这可以通过cast或通过新delegate类型创建指定的委托来实现,如下所示:

_dispatcher.BeginInvoke(
     new Action<MyClass>((sender) => { DoSomething(); }),
     new object[] { this } 
  );

Also, as SLaks points out, Dispatcher.BeginInvoke takes a params array, so you can just write:

而且,正如SLaks指出的,调度员。BeginInvoke使用一个params数组,所以您可以只写:

_dispatcher.BeginInvoke(
     new Action<MyClass>((sender) => { DoSomething(); }),
     this
  );

Or, if DoSomething is a method on this object itself:

或者,如果DoSomething是这个对象本身的一个方法:

_dispatcher.BeginInvoke(new Action(this.DoSomething));

#2


60  

Shorter:

短:

_dispatcher.BeginInvoke((Action)(() => DoSomething()));

#3


5  

Using Inline Lambda...

使用内联λ……

Dispatcher.BeginInvoke((Action)(()=>{
  //Write Code Here
}));

#4


5  

If you reference System.Windows.Presentation.dll from your project and add using System.Windows.Threading then you can access an extension method that allows you to use the lambda syntax.

如果你引用System.Windows.Presentation。使用System.Windows从项目中添加dll。线程化之后,您可以访问允许使用lambda语法的扩展方法。

using System.Windows.Threading;

...

Dispatcher.BeginInvoke(() =>
{
});

#5


0  

We create extension methods for this. E.g.

我们为此创建扩展方法。如。

public static void BeginInvoke(this Control control, Action action)
    => control.BeginInvoke(action);

Now we can call it from within a form: this.BeginInvoke(() => { ... }).

现在我们可以从表单中调用它:this.BeginInvoke() =>{…})。