Using Combinators
The Task
class has other interesting usages, such as managing concurrent operations. This is possible because of two methods, Task.WhenAll
and Task.WhenAny
, also known as combinators. Task.WhenAll
creates a task that will complete when all the supplied tasks complete; Task.WhenAny
creates a task that will complete when at least one of the supplied tasks completes. For example, imagine you want to download multiple RSS feeds information from a website. Instead of using Await
against individual tasks to complete, you can use Task.WhenAll
to continue only after all tasks have completed. The following code provides an example of concurrent download of RSS feeds from the Microsoft Channel 9 feed used previously:
Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.