Dispatcher.BeginInvoke: Cannot convert lambda to System.Delegate

Micah picture Micah · Feb 8, 2011 · Viewed 45.3k times · Source

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

BeginInvoke(Delegate method, params object[] args)

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

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

It's giving me a compiler error saying that I

can't convert the lambda to a 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?

Answer

Erwin Mayer picture Erwin Mayer · Oct 6, 2011

Shorter:

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