我试图让进度条的进度随着WebClient
下载进度的变化而改变。这段代码仍然会下载文件,但当我调用startDownload()
时,窗口会在下载文件时冻结。我希望用户能够在启动画面加载时看到进度的变化。有没有办法修复这个问题,使用户可以看到progressBar2
的进度变化?
private void startDownload()
{
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri("http://joshua-ferrara.com/luahelper/lua.syn"), @"C:\LUAHelper\Syntax Files\lua.syn");
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
label2.Text = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;
progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
label2.Text = "Completed";
}
startDownload()
。但这不应该导致窗口冻结,对吧? - Josh FerraraDownloadProgressChangedEventArgs
已经有e.ProgressPercentage
供你使用了。 - David Sherret