使用带有进度报告的异步/等待下载和解压缩 zip 文件
Download & extract zip file using Async / Await with progress reporting
我想实现一个可重复使用的 class,它将有助于下载给定的 zip 文件并解压缩它,同时使用 C# 5 await/async 功能报告进度。
我对此很陌生,目前正在努力解决这个问题。到目前为止,这是我的安装程序class:
class Installer
{
Button saveButton;
ProgressBar progressBar;
Label statusLabel;
Boolean downloadDone;
public Installer(Button _saveButton, ProgressBar _progressBar, Label _statusLabel)
{
saveButton = _saveButton;
progressBar = _progressBar;
statusLabel = _statusLabel;
}
public async void Start(string fileUrl)
{
saveButton.BeginInvoke((Action)(() => {
saveButton.Enabled = false;
}));
Task<bool> downloadArchiveTask = DownloadArchiveAsync(fileUrl);
bool downloadArchiveDone = await downloadArchiveTask;
if (downloadArchiveDone)
statusLabel.BeginInvoke((Action)(() => {
statusLabel.Text = "Download Completed";
}));
}
async Task<bool> DownloadArchiveAsync(string fileUrl)
{
var downloadLink = new Uri(fileUrl);
var saveFilename = Path.GetFileName(downloadLink.AbsolutePath);
DownloadProgressChangedEventHandler DownloadProgressChangedEvent = (s, e) =>
{
progressBar.BeginInvoke((Action)(() => {
progressBar.Value = e.ProgressPercentage;
}));
var downloadProgress = string.Format("{0} MB / {1} MB",
(e.BytesReceived / 1024d / 1024d).ToString("0.00"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));
statusLabel.BeginInvoke((Action)(() => {
statusLabel.Text = "Downloading " + downloadProgress + " ...";
}));
};
AsyncCompletedEventHandler AsyncCompletedEvent = (s, e) =>
{
// Todo: Extract
downloadDone = true;
};
using (WebClient webClient = new WebClient())
{
webClient.DownloadProgressChanged += DownloadProgressChangedEvent;
webClient.DownloadFileCompleted += AsyncCompletedEvent;
webClient.DownloadFileAsync(downloadLink, saveFilename);
}
while (!downloadDone) ;
return true;
}
}
我是这样使用的:
(new Installer(startBtn, progressBar, statusLabel)).Start("http://nginx.org/download/nginx-1.9.4.zip");
我不完全确定我是否正确实施了这一点。 Visual studio 给我以下警告:
DownloadArchiveAsync - This async method lacks 'await' operators and will run synchronously.
另请注意;我目前没有正确的方法来检测下载何时完成,所以我使用 bool
和 while
循环 - 不确定是否也推荐这样做。
使用 async/await 异步下载 zip 文件并报告进度的正确方法是什么?
编辑
经过深入研究,我找到了一种可能的解决方案。我已经实现了这个方法:
async Task IsDownloadDone()
{
await Task.Run(() =>
{
while (!downloadDone) ;
});
}
并像这样更新 DownloadArchiveAsync
return:
await IsDownloadDone();
return true;
完整代码现在如下所示:http://pastebin.com/MuW0386K
这是实现它的正确方法吗?
你可以用这样的东西替换DownloadArchiveAsync
,它实际上会异步工作:
async Task<bool> DownloadArchiveAsync( string fileUrl )
{
var downloadLink = new Uri( fileUrl );
var saveFilename = System.IO.Path.GetFileName( downloadLink.AbsolutePath );
DownloadProgressChangedEventHandler DownloadProgressChangedEvent = ( s, e ) =>
{
progressBar.BeginInvoke( (Action)(() =>
{
progressBar.Value = e.ProgressPercentage;
}) );
var downloadProgress = string.Format( "{0} MB / {1} MB",
(e.BytesReceived / 1024d / 1024d).ToString( "0.00" ),
(e.TotalBytesToReceive / 1024d / 1024d).ToString( "0.00" ) );
statusLabel.BeginInvoke( (Action)(() =>
{
statusLabel.Text = "Downloading " + downloadProgress + " ...";
}) );
};
using (WebClient webClient = new WebClient())
{
webClient.DownloadProgressChanged += DownloadProgressChangedEvent;
await webClient.DownloadFileTaskAsync( downloadLink, saveFilename );
}
return true;
}
编辑: 我在 MSDN 中找到 DownloadFileTaskAsync
,让事情变得更漂亮。
方法上的async
表示此方法使用await。所以在'WebClient'.
中使用awaitable函数
我想实现一个可重复使用的 class,它将有助于下载给定的 zip 文件并解压缩它,同时使用 C# 5 await/async 功能报告进度。
我对此很陌生,目前正在努力解决这个问题。到目前为止,这是我的安装程序class:
class Installer
{
Button saveButton;
ProgressBar progressBar;
Label statusLabel;
Boolean downloadDone;
public Installer(Button _saveButton, ProgressBar _progressBar, Label _statusLabel)
{
saveButton = _saveButton;
progressBar = _progressBar;
statusLabel = _statusLabel;
}
public async void Start(string fileUrl)
{
saveButton.BeginInvoke((Action)(() => {
saveButton.Enabled = false;
}));
Task<bool> downloadArchiveTask = DownloadArchiveAsync(fileUrl);
bool downloadArchiveDone = await downloadArchiveTask;
if (downloadArchiveDone)
statusLabel.BeginInvoke((Action)(() => {
statusLabel.Text = "Download Completed";
}));
}
async Task<bool> DownloadArchiveAsync(string fileUrl)
{
var downloadLink = new Uri(fileUrl);
var saveFilename = Path.GetFileName(downloadLink.AbsolutePath);
DownloadProgressChangedEventHandler DownloadProgressChangedEvent = (s, e) =>
{
progressBar.BeginInvoke((Action)(() => {
progressBar.Value = e.ProgressPercentage;
}));
var downloadProgress = string.Format("{0} MB / {1} MB",
(e.BytesReceived / 1024d / 1024d).ToString("0.00"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));
statusLabel.BeginInvoke((Action)(() => {
statusLabel.Text = "Downloading " + downloadProgress + " ...";
}));
};
AsyncCompletedEventHandler AsyncCompletedEvent = (s, e) =>
{
// Todo: Extract
downloadDone = true;
};
using (WebClient webClient = new WebClient())
{
webClient.DownloadProgressChanged += DownloadProgressChangedEvent;
webClient.DownloadFileCompleted += AsyncCompletedEvent;
webClient.DownloadFileAsync(downloadLink, saveFilename);
}
while (!downloadDone) ;
return true;
}
}
我是这样使用的:
(new Installer(startBtn, progressBar, statusLabel)).Start("http://nginx.org/download/nginx-1.9.4.zip");
我不完全确定我是否正确实施了这一点。 Visual studio 给我以下警告:
DownloadArchiveAsync - This async method lacks 'await' operators and will run synchronously.
另请注意;我目前没有正确的方法来检测下载何时完成,所以我使用 bool
和 while
循环 - 不确定是否也推荐这样做。
使用 async/await 异步下载 zip 文件并报告进度的正确方法是什么?
编辑
经过深入研究,我找到了一种可能的解决方案。我已经实现了这个方法:
async Task IsDownloadDone()
{
await Task.Run(() =>
{
while (!downloadDone) ;
});
}
并像这样更新 DownloadArchiveAsync
return:
await IsDownloadDone();
return true;
完整代码现在如下所示:http://pastebin.com/MuW0386K
这是实现它的正确方法吗?
你可以用这样的东西替换DownloadArchiveAsync
,它实际上会异步工作:
async Task<bool> DownloadArchiveAsync( string fileUrl )
{
var downloadLink = new Uri( fileUrl );
var saveFilename = System.IO.Path.GetFileName( downloadLink.AbsolutePath );
DownloadProgressChangedEventHandler DownloadProgressChangedEvent = ( s, e ) =>
{
progressBar.BeginInvoke( (Action)(() =>
{
progressBar.Value = e.ProgressPercentage;
}) );
var downloadProgress = string.Format( "{0} MB / {1} MB",
(e.BytesReceived / 1024d / 1024d).ToString( "0.00" ),
(e.TotalBytesToReceive / 1024d / 1024d).ToString( "0.00" ) );
statusLabel.BeginInvoke( (Action)(() =>
{
statusLabel.Text = "Downloading " + downloadProgress + " ...";
}) );
};
using (WebClient webClient = new WebClient())
{
webClient.DownloadProgressChanged += DownloadProgressChangedEvent;
await webClient.DownloadFileTaskAsync( downloadLink, saveFilename );
}
return true;
}
编辑: 我在 MSDN 中找到 DownloadFileTaskAsync
,让事情变得更漂亮。
async
表示此方法使用await。所以在'WebClient'.