Skip to content

Instantly share code, notes, and snippets.

@jlennox
Created July 23, 2015 14:24
Show Gist options
  • Save jlennox/b73b531446e517c1bb44 to your computer and use it in GitHub Desktop.
Save jlennox/b73b531446e517c1bb44 to your computer and use it in GitHub Desktop.
Task AnarcodesWebClientLoop(string[] urls) {
using (var entryGate = new SemaphoreSlim(10, 10)) {
var requests = urls.Select(async url => {
await entryGate.WaitAsync();
var didError = false;
string errorMessage = null;
try {
var req = WebRequest.Create(url);
using (var res = (HttpWebResponse)await req.GetResponseAsync())
{
didError = (int)res.StatusCode < 200 || (int)res.StatusCode >= 300);
using (var reader = new StreamReader(res.GetResponseStream()))
errorMessage = await reader.ReadToEndAsync();
}
} catch (Exception e) {
didError = true;
if (errorMessage == null)
errorMessage = e.Message;
}
// do things with didError and errorMessage here.
}).ToArray();
return Task.WhenAll(requests);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment