Skip to content

Instantly share code, notes, and snippets.

@damirarh
Created July 20, 2013 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damirarh/6046232 to your computer and use it in GitHub Desktop.
Save damirarh/6046232 to your computer and use it in GitHub Desktop.
private Task<string> DownloadStringAsync(Uri uri)
{
var taskSource = new TaskCompletionSource<string>();
var webClient = new WebClient();
webClient.DownloadStringCompleted += (sender, args) =>
{
try
{
if (args.Error != null)
{
taskSource.SetException(args.Error);
}
else if (args.Cancelled)
{
taskSource.SetCanceled();
}
else
{
taskSource.SetResult(args.Result);
}
}
finally
{
webClient.Dispose();
}
};
webClient.DownloadStringAsync(uri);
return taskSource.Task;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment