Created
January 10, 2015 05:45
-
-
Save AlexArchive/fdf2469a47765cee5a13 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 static async Task<string> DownloadStringWithTimeout(string location) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
var downloadTask = client.GetStringAsync(location); | |
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(3)); | |
var completedTask = await Task.WhenAny(downloadTask, timeoutTask); | |
if (completedTask == timeoutTask) | |
return null; | |
return await downloadTask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment