Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is an <a href="http://www.hanselman.com/blog/BackToParallelBasicsDontBlockYourThreadsMakeAsyncIOWorkForYou.aspx" rel="nofollow">article by Scott Hanselman</a> in which he describes how to do non blocking requests. Scrolling to the end of it, there is a <code>public Task&lt;bool&gt; ValidateUrlAsync(string url)</code> method.</p> <p>You could modify it like this (could be more robust about response reading)</p> <pre><code>public Task&lt;string&gt; GetAsync(string url) { var tcs = new TaskCompletionSource&lt;string&gt;(); var request = (HttpWebRequest)WebRequest.Create(url); try { request.BeginGetResponse(iar =&gt; { HttpWebResponse response = null; try { response = (HttpWebResponse)request.EndGetResponse(iar); using(var reader = new StreamReader(response.GetResponseStream())) { tcs.SetResult(reader.ReadToEnd()); } } catch(Exception exc) { tcs.SetException(exc); } finally { if (response != null) response.Close(); } }, null); } catch(Exception exc) { tcs.SetException(exc); } return tsc.Task; } </code></pre> <p>So with this in hand, you could then use it like this</p> <pre><code>var urls=new[]{"url1","url2"}; var tasks = urls.Select(GetAsync).ToArray(); var completed = Task.Factory.ContinueWhenAll(tasks, completedTasks =&gt;{ foreach(var result in completedTasks.Select(t=&gt;t.Result)) { Console.WriteLine(result); } }); completed.Wait(); //anything that follows gets executed after all urls have finished downloading </code></pre> <p>Hope this puts you in the right direction.</p> <p>PS. this is probably as clear as it can get without using async/await</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload