The motivation for implementing a "true" async method is clear, as stated by @Steven_Cleary, but sometimes you have legacy code you cannot simply make async. For those in desperate need for keeping the old interface: Use Task.Run
. E.g. when you had
IAsyncResult ar = someDelegate.BeginInvoke(state, null, null);
and use properties of ar
in order to see when the task is finished, you are in luck because the equivalent with Tasks is:
Task task = Task.Run(() => someDelegate(state));
The good news is that the Task
class implements IAsyncResult
such that you can recycle your existing code.
Later when you know the delegate has finished, you might have called
someDelegate.EndInvoke();
which is neither supported by .NET Core. This can the be replaced by
task.Wait();
It eventually throws exceptions your delegate has thrown, just like EndInvoke
.
This worked in our migration to .NET Core.