Moffen
Moffen

Reputation: 2013

Wait for all tasks when tasks run async methods

I have been trying to combine the results of several questions on waiting for multiple threads, but my call to await Task.WhenAll(Tasks); is being passed before all tasks are finished!

List<Task> tasks = new List<Task>();
foreach (DownloadingFile file in downloadingFiles)
{
    Task t = new Task(async delegate { await file.PollDownload(); } );
    tasks.Add(t);
    t.Start();
}
await Task.WhenAll(tasks);
internal async Task PollDownload()
{
    string localFile = "pathToDestination";
    WebRequest headerRequest = WebRequest.Create(uri);
    headerRequest.Method = "HEAD";
    HttpWebResponse response = null;
    try
    {
        response = (HttpWebResponse) await headerRequest.GetResponseAsync();
    }
    catch
    {
        Thread.Sleep(500);
        try
        {
            response = (HttpWebResponse)await headerRequest.GetResponseAsync();
        }
        catch
        {
            response = null;
        }
    }
    if (response == null)
    {
        state = DOWNLOAD_STATE.FAILED;
        return;
    }
    totalBytes = response.ContentLength;
    state = DOWNLOAD_STATE.WAITING_START;
    //Check to see whether a local copy of the remote file exists
    if (File.Exists(localFile))
    {
        DateTime localFileUpdated = File.GetCreationTimeUtc(localFile);
        DateTime onlineFileUpdated = response.LastModified;
        // If the download date of the local file comes after the modification date of the remote copy
        // then we do not need to take any further action here.
        if (onlineFileUpdated <= localFileUpdated)
        {
            state = DOWNLOAD_STATE.SKIPPED;
        }
        //Otherwise we should remove the local copy and proceed with the download
        else
        {
            File.Delete(localFile);
        }
    }
    return;
}

I assume this is because the Tasks include async delegate methods? I don't WANT them to be async but they need to be because of the PollDownload() awaits other async methods ( though they can probably be changed to synchronous methods here they cannot be changed to synchronous methods in another instance of this problem so I still need a solution).

Upvotes: 0

Views: 307

Answers (1)

SLaks
SLaks

Reputation: 888303

new Task() accepts an Action, so you're passing an async void that does not wait for its result.

You should use Task.Run() instead, which also accepts Func<Task>.

Upvotes: 4

Related Questions