Anonymous functions (lambda expressions and anonymous methods) must be converted to a specific type of delegate, while Dispatcher.BeginInvoke just accepts a Delegate . There are two options for this ...
Continue to use the existing BeginInvoke call, but specify the delegate type. There are various approaches here, but I usually extract the anonymous function in the previous statement:
Action action = delegate() { this.Log.Add(...); }; Dispatcher.BeginInvoke(action);
Write an extension method on Dispatcher that accepts Action instead of Delegate :
public static void BeginInvokeAction(this Dispatcher dispatcher, Action action) { Dispatcher.BeginInvoke(action); }
Then you can call the extension method with implicit conversion
this.Dispatcher.BeginInvokeAction( delegate() { this.Log.Add(...); });
I would also recommend using lambda expressions instead of anonymous methods:
Dispatcher.BeginInvokeAction(() => this.Log.Add(...));
EDIT: As noted in the comments, Dispatcher.BeginInvoke got overloaded in .NET 4.5, which takes Action directly, so you don't need an extension method in this case.
Jon skeet
source share