gpt4 book ai didi

c# - await Task.WhenAll 看似完成,但它永远不会到达下一行代码

转载 作者:行者123 更新时间:2023-11-30 20:31:14 26 4
gpt4 key购买 nike

我有一个简单的文件下载器,我从服务器获取一些文件,然后显示一条消息,告知它是成功还是失败。这是代码:

public async Task GetFiles(IEnumerable<string> urlList)
{
//some variable declaring, setting etc.

await Task.WhenAll(urlList.Select(url => Task.Run(() => DownloadFile(url, dir, count++))).ToList());
//problem lies here, the WhenAll never completes
if (fileDownloadError)
{
Directory.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "downloads"));
MessageBox.Show("Fail");
return;
}
MessageBox.Show("Success");
}

internal async Task DownloadFile(string url, string dir, int count)
{
string filename = HelperClass.GetFilenameFromUrl(url);
long a = 0;
newList.Add(a);
using (var client = new WebClient())
{
TempDownload tp = new TempDownload()
{
id = count
};

client.DownloadProgressChanged += new DownloadProgressChangedEventHandler((sender, e) => ProgressChanged(sender, e, tp));
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

await client.DownloadFileTaskAsync(url, string.Concat(dir, "\\", filename));
}
}

我在 DownloadProgressChanged 事件中更改的 ProgressBar 变为 100%,文件已正确下载,但 WhenAll 之后的代码从未执行,也就是说,它永远不会在 if (fileDownloadError) 上遇到断点,没有显示 MessageBox 等。我在这里遗漏了什么吗?

最佳答案

由于死锁,断点永远不会命中 if 语句。将 ConfigureAwait(false) 添加到所有等待。前-

await Task.WhenAll(urlList.Select(url => Task.Run(() => DownloadFile(url, dir, count++))).ToList()).ConfigureAwait(false);

还有

await client.DownloadFileTaskAsync(url, string.Concat(dir, "\\", filename)).ConfigureAwait(false);

解决这个问题的另一种选择是不在 UI 线程上使用 Wait 或 Result。使用异步等待

前-

private async void button1_Click(object sender, EventArgs e)
{
await GetFiles();
}

详情,

https://msdn.microsoft.com/en-us/magazine/gg598924.aspx

https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

关于c# - await Task.WhenAll 看似完成,但它永远不会到达下一行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com