My objective is to load multiple links at the same time and create a task for each of them.
The task will call an async method that will parse the links and returns sublinks, which in return will be parsed (using WebBrowser) and then they return a download link.
The first async method will call 2 subsequent methods for that work to be done.
My problem is Task.Factory.ContinueWhenAll would return only when the all first method finish, and won't wait for the rest of the work to be done. I only want to continue when I have all download links ready which may need multiple webpage parsings before they are.
Currently my code is the following:
var tasks = new List<Task>();
for (var index = 0; index < items_checklist.CheckedItems.Count; index++)
{
var item = items_checklist.CheckedItems[index];
Task task = Task.Factory.StartNew(
() => GetMirrors(((Item) item).Value, ((Item) item).Text)
, CancellationToken.None
, TaskCreationOptions.None
, TaskScheduler.FromCurrentSynchronizationContext()
);
tasks.Add(task);
}
Task.Factory.ContinueWhenAll(tasks.ToArray(), GetLinks_Finished =>
{
SetLinksButtonText(@"Links Ready");
SetLinksButtonState(false);
SetDownloadButtonState(true);
Cursor.Current = DefaultCursor;
});
This will return when all GetMirrors finish but GetMirrors would call "tempbrowser_DocumentCompleted" (WebBrowser complete event) which in turn would call "LoadLinkIntoQueue" to load the download link into the queue.
I want ContinueWhenAll to resume when all LoadLinkIntoQueue are executed.
What is my logic missing?