How to wrap existing async method that accepts callback function as parameter into Task Parallel Library-compatible method?
// Existing method
void DoAsync(Action<string> callback) {
...
}
// The desired method should have similar prototype
Task<string> DoAsync() {
// Internally this method should call existing
// version of DoAsync method (see above)
}
I'm assuming that your existing
DoAsync
method will run asynchronously.In that case, you can wrap it like this:
I don't see how your existing
DoAsync
method reports asynchronous errors. You can useTaskCompletionSource<T>.TrySetException
to report an asynchronous error if necessary.