Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Typically you use WaitAny to wait for one task when you don't care about the results of any of the others. For example if you just cared about the first image that happened to get returned.</p> <p>How about this instead.</p> <p>This creates two tasks, one which loads images and adds them to a blocking collection. The second task waits on the collection and processes any images added to the queue. When all the images are loaded the first task closes the queue down so the second task can shut down.</p> <pre><code>using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Net; using System.Threading.Tasks; namespace ClassLibrary1 { public class Class1 { readonly string _path = Directory.GetCurrentDirectory(); public void Demo() { IList&lt;string&gt; listOfUrls = new List&lt;string&gt;(); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/editicon.gif"); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/favorite-star-on.gif"); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/arrow_dsc_green.gif"); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/editicon.gif"); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/favorite-star-on.gif"); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/arrow_dsc_green.gif"); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/editicon.gif"); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/favorite-star-on.gif"); listOfUrls.Add("http://i3.codeplex.com/Images/v16821/arrow_dsc_green.gif"); BlockingCollection&lt;Image&gt; images = new BlockingCollection&lt;Image&gt;(); Parallel.Invoke( () =&gt; // Task 1: load the images { Parallel.For(0, listOfUrls.Count, (i) =&gt; { Image img = RetrieveImages(listOfUrls[i], i); img.Tag = i; images.Add(img); // Add each image to the queue }); images.CompleteAdding(); // Done with images. }, () =&gt; // Task 2: Process images serially { foreach (var img in images.GetConsumingEnumerable()) { string newPath = Path.Combine(_path, String.Format("{0}_rot.png", img.Tag)); Console.WriteLine("Rotating image {0}", img.Tag); img.RotateFlip(RotateFlipType.RotateNoneFlipXY); img.Save(newPath); } }); } public Image RetrieveImages(string url, int i) { using (WebClient client = new WebClient()) { string fileName = Path.Combine(_path, String.Format("{0}.png", i)); Console.WriteLine("Downloading {0}...", url); client.DownloadFile(url, Path.Combine(_path, fileName)); Console.WriteLine("Saving {0} as {1}.", url, fileName); return Image.FromFile(Path.Combine(_path, fileName)); } } } } </code></pre> <p>WARNING: The code doesn't have any error checking or cancelation. It's late and you need something to do right? :)</p> <p>This is an example of the pipeline pattern. It assumes that getting an image is pretty slow and that the cost of locking inside the blocking collection isn't going to cause a problem because it happens relatively infrequently compared to the time spent downloading images.</p> <p>Our book... You can read more about this and other patterns for parallel programming at <a href="http://parallelpatterns.codeplex.com/" rel="noreferrer">http://parallelpatterns.codeplex.com/</a> Chapter 7 covers pipelines and the accompanying examples show pipelines with error handling and cancellation.</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.
 

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