Dispatcher BeginInvoke Syntax
Answer by Jon Skeet is very good but there are other possibilities. I prefer "begin invoke new action" which is easy to read and to remember for me.
private void OnSaveCompleted(IAsyncResult result)
{
Dispatcher.BeginInvoke(new Action(() =>
{
context.EndSaveChanges(result);
}));
}
or
private void OnSaveCompleted(IAsyncResult result)
{
Dispatcher.BeginInvoke(new Action(delegate
{
context.EndSaveChanges(result);
}));
}
or
private void OnSaveCompleted(IAsyncResult result)
{
Dispatcher.BeginInvoke(new Action(() => context.EndSaveChanges(result)));
}
If your method does not require parameters, this is the shortest version I've found
Application.Current.Dispatcher.BeginInvoke((Action)MethodName);
The problem is that the compiler doesn't know what kind of delegate you're trying to convert the lambda expression to. You can fix that either with a cast, or a separate variable:
private void OnSaveCompleted(IAsyncResult result)
{
Dispatcher.BeginInvoke((Action) (() =>
{
context.EndSaveChanges(result);
}));
}
or
private void OnSaveCompleted(IAsyncResult result)
{
Action action = () =>
{
context.EndSaveChanges(result);
};
Dispatcher.BeginInvoke(action);
}