Are regular iterator blocks (i.e. \"yield return\") incompatible with \"async\" and \"await\"?
This gives a good idea of what I\'m trying to do:
asyn
First of all, keep in mind that the Async stuff is not finished. The C# team still has a long way to go before C# 5 is released.
That being said, I think you may want to gather the tasks that are being fired off in the DownloadAllHtml function in a different way.
For example, you can use something like this:
IEnumerable> DownloadAllUrl(string[] urls)
{
foreach(var url in urls)
{
yield return DownloadHtmlAsync(url);
}
}
async Task DownloadHtmlAsync(url)
{
// Do your downloading here...
}
Not that the DownloadAllUrl function is NOT an async call. But, you can have the async call implemented on another function (i.e. DownloadHtmlAsync).
The Task Parallel Library has the .ContinueWhenAny and .ContinueWhenAll functions.
That can be used like this:
var tasks = DownloadAllUrl(...);
var tasksArray = tasks.ToArray();
var continuation = Task.Factory.ContinueWhenAll(tasksArray, completedTasks =>
{
completedtask
});
continuation.RunSynchronously();