This is the fix for my initial code, that makes it work as expected.
I had to define a closure for the callback
AsyncCallback callback;
Then I had to pass the callback
to the closure from the beginMethod
:
private IAsyncResult beginMethod(AsyncCallback callback, object state)
{
this.callback = callback;
return Task.FromResult(true);
}
and finally invoke it from the event (i.e. the Command
method in the MVVM for WPF)
private async void RunNext()
{
IsBusy = true;
ProgressMessage = "Wait 10 seconds...";
await workToDo();
ProgressMessage = "Work done!";
IsBusy = false;
if (callback != null)
{
callback.Invoke(Task.FromResult(true));
}
}