I have an existing method with return type IAsyncResult from older code, how can I call an async Task from within this method? For example:
public IAsyncResult BeginXXX()
{
// I want to call my async Task here, but it cannot compile like this:
var value = await DoMyTaskAsync();
DoSomethingWithValue(value);
// existing code
return new SomeOtherAsyncResult();
}
Edit: The solution would preferably not have to refactor the method signature, that would involve work beyond the scope.
This is a common problem. You solve this using
TaskCompletionSource.Let's say you have a legacy class and it has the old-style async methods
BeginBarandEndBar. TheBeginBaraccepts anAsyncCallbackdelegate.See interop with other asynchronous patterns