What would be the best way to convert / migrate a “classic” asynchronous method that uses a callback for something that returns a (expected) task?
For example, given the following method:
public void GetStringFromUrl(string url, Action<string> onCompleted);
The only way I know is to include this in the method returning the task:
public Task<string> GetStringFromUrl(string url) { var t = new TaskCompletionSource<string>(); GetStringFromUrl(url, s => t.TrySetResult(s)); return t.Task; }
Is this the only way to achieve this?
And is there a way to wrap the GetStringFromUrl (url, callback) call in the task itself (i.e. the call itself will be executed inside the task instead of synchronous)
Philippe Leybaert Aug 09 2018-12-12T00: 00Z
source share