Skip to content

Instantly share code, notes, and snippets.

@Rast1234
Created July 31, 2014 16:00
Show Gist options
  • Save Rast1234/b5a9f24668102d9fa05f to your computer and use it in GitHub Desktop.
Save Rast1234/b5a9f24668102d9fa05f to your computer and use it in GitHub Desktop.
Mix callbacks and async
//For example, given the following method:
public void GetStringFromUrl(string url, Action<string> onCompleted);
//The only way I know of to wrap this into a method returning a task is:
public Task<string> GetStringFromUrl(string url)
{
var t = new TaskCompletionSource<string>();
GetStringFromUrl(url, s => t.TrySetResult(s));
return t.Task;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment