Skip to content

anonymous /gist:4478284
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.