84

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?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Micah
  • 111,873
  • 86
  • 233
  • 325
  • Does this answer your question? [Cannot convert lambda expression to type 'System.Delegate'](https://stackoverflow.com/questions/9549358/cannot-convert-lambda-expression-to-type-system-delegate) – StayOnTarget Jan 22 '21 at 12:51

5 Answers5

76

Shorter:

_dispatcher.BeginInvoke((Action)(() => DoSomething()));
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
73

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:

_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:

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

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

_dispatcher.BeginInvoke(new Action(this.DoSomething));
Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 3
    But doesn't (x) => { DoSomething(); } match the signature of the delegate? I thought that's all I should have to specify. – Micah Feb 08 '11 at 17:53
  • @Micah: System.Delegate doesn't have a signature - it's just "any delegate" at all. You need to provide it a delegate type with a sig. that matches your usage. – Reed Copsey Feb 08 '11 at 17:54
  • @Reed But if in place of MyMethod(Action action) (and Action is a delegate) I can call MyMethod(() => { DoSomething(); }); Why can't I do the same thing for BeginInvoke? – Micah Feb 08 '11 at 17:56
  • 16
    @Micah: There actually *isn't* a signature for the delegate, which is what causes the issue. `Invoke` and `BeginInvoke` take a generic `Delegate` object, which can represent a method of any signature. Under normal circumstances (where a delegate is strongly typed to a particular signature), the compiler can infer the specific delegate type. This is why you're able to get away with omitting the delegate type in other scenarios. However, since there *is* no actual delegate type here, the compiler does not have a reasonable basis (or, really, even a *means*) to use to select a delegate type. – Adam Robinson Feb 08 '11 at 17:56
  • 2
    @Micah: Because BeginInvoke isn't declared as BeginInvoke(Action ..), but rather BeginInvoke(System.Delegate, ..) This allows it to use ANY delegate type, but you must specify it explicitly. – Reed Copsey Feb 08 '11 at 18:00
  • I'd love to know why they did this! Invoke works in a sane way: Invoke(()=>{}); Why does BeginInvoke force you to do all this? I'll give all DispatcherObjects a comfortable extension method anyways, but still, WHY? – ASA Jul 15 '15 at 09:04
10

Using Inline Lambda...

Dispatcher.BeginInvoke((Action)(()=>{
  //Write Code Here
}));
JWP
  • 6,672
  • 3
  • 50
  • 74
7

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.

using System.Windows.Threading;

...

Dispatcher.BeginInvoke(() =>
{
});
logicnet.dk
  • 193
  • 1
  • 7
5

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(() => { ... }).

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467