Created
July 20, 2013 19:57
-
-
Save damirarh/6046232 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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