Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2013 20:52
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 anonymous/4478284 to your computer and use it in GitHub Desktop.
Save anonymous/4478284 to your computer and use it in GitHub Desktop.
async private void Download_Click_1(object sender, RoutedEventArgs e)
{
string[] files =
{
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg",
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg"
};
List<Task<Byte[]>> tasks = new List<Task<Byte[]>>();
for (int idx = 0; idx < files.Count(); idx++)
{
var file = files[idx];
var taskStart = DownloadImageData(new Uri(file), null);
tasks.Add(taskStart);
if (((idx > 0) && (idx % 5 == 0)) || (idx == files.Count() - 1))
{
await Task.WhenAll(tasks);
tasks.Clear();
}
}
}
private async Task<Byte[]> DownloadImageData(Uri url, Object userState)
{
Byte[] contentBytes = null;
using (HttpClientHandler _handler = new HttpClientHandler())
{
_handler.AllowAutoRedirect = true;
_handler.MaxAutomaticRedirections = 4;
using (HttpClient httpClient = new HttpClient(_handler))
{
httpClient.DefaultRequestHeaders.ExpectContinue = false;
httpClient.DefaultRequestHeaders.Add("Keep-Alive", "false");
try
{
// Exception IS THROWN AT LINE BELOW
contentBytes = await httpClient.GetByteArrayAsync(url.OriginalString);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Download Failed :" + ex.Message);
throw ex;
}
}
_handler.Dispose();
}
return contentBytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment