鉴于这段代码,我注意到我的 UI 有一段时间被阻塞了(Windows 甚至弹出一条消息说应用程序没有响应。
using (var zip = await downloader.DownloadAsZipArchive(downloadUrl))
{
var temp = FileUtils.GetTempDirectoryName();
zip.ExtractToDirectory(temp); // BLOCKING CALL
if (Directory.Exists(folderPath))
{
Directory.Delete(folderPath, true);
}
var firstChild = Path.Combine(temp, folderName);
Directory.Move(firstChild, folderPath);
Directory.Delete(temp);
}
经过一些检查,我发现那一行说:
zip.ExtractToDirectory(temp);
是罪魁祸首。
我认为把它变成就足以让它工作了:
await Task.Run(() => zip.ExtractToDirectory(temp));
但是……这是解决这个问题的好方法吗?
我有 System.Reactive
的背景(我完全参与响应式编程),我想知道是否有更优雅的方法来处理这个问题。
最佳答案
在 Rx 中这样做有点讨厌。结合 Task<IDisposable>
很粗糙。这是我得到的:
Observable
.FromAsync(() => downloader.DownloadAsZipArchive(downloadUrl))
.SelectMany(z =>
Observable
.Using(() => z, zip => Observable.Start(() =>
{
var temp = FileUtils.GetTempDirectoryName();
zip.ExtractToDirectory(temp); // BLOCKING CALL
if (Directory.Exists(folderPath))
{
Directory.Delete(folderPath, true);
}
var firstChild = Path.Combine(temp, folderName);
Directory.Move(firstChild, folderPath);
Directory.Delete(temp);
})))
.Subscribe();
关于c# - 如何处理使 UI 无响应的同步(阻塞)调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54564325/