Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the past I've implemented this using an iterator method: every time you want another URL requested, you use "yield return" to pass control back to the main program. Once the request finishes, the main program calls back into your iterator to execute the next piece of work.</p> <p>You're effectively using the C# compiler to write a state machine for you. The advantage is that you can write normal-looking C# code in the iterator method to drive the whole thing.</p> <pre><code>using System; using System.Collections.Generic; using System.Net; class Program { static void onEx(Exception ex) { Console.WriteLine(ex.ToString()); } static IEnumerable&lt;Uri&gt; Downloader(Func&lt;DownloadDataCompletedEventArgs&gt; getLastResult) { Uri url1 = new Uri(Console.ReadLine()); Uri url2 = new Uri(Console.ReadLine()); string someData = Console.ReadLine(); yield return url1; DownloadDataCompletedEventArgs res1 = getLastResult(); yield return new Uri(url2.ToString() + "?data=" + res1.Result); DownloadDataCompletedEventArgs res2 = getLastResult(); Console.WriteLine(someData + res2.Result); } static void StartNextRequest(WebClient webThingy, IEnumerator&lt;Uri&gt; enumerator) { if (enumerator.MoveNext()) { Uri uri = enumerator.Current; try { Console.WriteLine("Requesting {0}", uri); webThingy.DownloadDataAsync(uri); } catch (Exception ex) { onEx(ex); } } else Console.WriteLine("Finished"); } static void Main() { DownloadDataCompletedEventArgs lastResult = null; Func&lt;DownloadDataCompletedEventArgs&gt; getLastResult = delegate { return lastResult; }; IEnumerable&lt;Uri&gt; enumerable = Downloader(getLastResult); using (IEnumerator&lt;Uri&gt; enumerator = enumerable.GetEnumerator()) { WebClient webThingy = new WebClient(); webThingy.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e) { if (e.Error == null) { lastResult = e; StartNextRequest(webThingy, enumerator); } else onEx(e.Error); }; StartNextRequest(webThingy, enumerator); } Console.WriteLine("Keeping process alive"); Console.ReadLine(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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