Task.wait不像我想象的那样工作

时间:2014-08-22 16:15:14

标签: c# multithreading task

我正在尝试下载文件,等待文件完成下载,然后再读取文件。我有以下方法来执行此操作:

private async Task startDownload(string link, string savePath)
{      
        WebClient client = new WebClient();
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
        await client.DownloadFileTaskAsync(new Uri(link), savePath);              
}

private void checkUpdateButton_Click(object sender, EventArgs e)
{           
    Task task = Task.Factory.StartNew(() => startDownload(versionLink, versionSaveTo));
    task.Wait();

    if (task.IsCompleted)
    {
        checkVersion();
    }
}

checkVersion()方法读取已下载的文件。这引发了IOException说该文件正被其他东西使用而无法读取。我认为有task.Wait会阻止方法的其余部分执行,直到任务完成?

3 个答案:

答案 0 :(得分:1)

Task.Wait将阻止当前线程(在本例中为UI线程)并等待任务完成。在这种情况下,任务正在完成并出现错误,因此Task.Wait会将该错误包含在AggregateException中。

正如其他人所说,您应该使用await代替Wait。另外,DownloadFileCompleted因为您使用DownloadFileTaskAsync(而不是DownloadFileAsync)而没有意义。并且StartNew是不必要的,因为下载是异步的。

哦,让我们处理WebClient并确保我们的命名约定遵循Task-based Asynchronous Pattern

private async Task startDownloadAsync(string link, string savePath)
{      
  using (var client = new WebClient())
  {
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
    await client.DownloadFileTaskAsync(new Uri(link), savePath);              
  }
}

private async void checkUpdateButton_Click(object sender, EventArgs e)
{           
  await startDownloadAsync(versionLink, versionSaveTo);
  checkVersion();
}

答案 1 :(得分:0)

函数startDownload已经是异步,因此它将启动任务并立即返回。在调用checkVersion()之前,您可以使用ContinueWith确保任务完成。

    private void checkUpdateButton_Click(object sender, EventArgs e)
    {
        var task = startDownload(versionLink, versionSaveTo);
        task.ContinueWith((x) => checkVersion());
    }

Servy指出,另一种选择是在Click事件中使用async / await。

    private async void checkUpdateButton_Click(object sender, EventArgs e)
    {
        await startDownload(versionLink, versionSaveTo);
        checkVersion();
    }

答案 2 :(得分:-1)

您需要等待Task.Factory.StartNew(...)调用,这样就不会阻止UI线程。

private async void button1_Click(object sender, EventArgs e)
{
    Task task = await Task.Factory.StartNew(() => startDownload("http://www.zwaldtransport.com/images/placeholders/placeholder1.jpg", "" + "sd.jpg"));
}

private async Task startDownload(string link, string savePath)
{
    WebClient client = new WebClient();
    client.DownloadProgressChanged += Client_DownloadProgressChanged;
    client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
    await client.DownloadFileTaskAsync(new Uri(link), savePath);
}

private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
            checkVersion();
    Console.WriteLine("Done, unless error or cancelled.");
}

private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine("Progress changed.");
}

图片占位符由Google图片和其他一些网站提供。