异步文件下载与进度条 [英] Asynchronous File Download with Progress Bar

查看:734
本文介绍了异步文件下载与进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图有一个进度条的进度变化的 Web客户端下载进度的变化。这code还是下载文件但当我打电话 startDownload()窗口冻结,因为它下载文件。我希望用户能够看到的进展变化闪屏载荷。有什么办法来解决这个问题,使用户可以看到 progressBar2 变化的进展如何?

 私人无效startDownload()
{
    Web客户端的客户端=新的WebClient();
    client.DownloadProgressChanged + =新DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
    client.DownloadFileCompleted + =新AsyncCompletedEventHandler(client_DownloadFileCompleted);
    client.DownloadFileAsync(新的URI(http://joshua-ferrara.com/luahelper/lua.syn),@C:\\ LUAHelper \\语法文件\\ lua.syn);
}
无效client_DownloadProgressChanged(对象发件人,DownloadProgressChangedEventArgs E)
{
    双bytesIn = double.Parse(e.BytesReceived.ToString());
    双totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
    双率= bytesIn / totalBytes * 100;
    label2.Text =已下载+ e.BytesReceived +的+ e.TotalBytesToReceive;
    progressBar1.Value = int.Parse(Math.Truncate(百分比)的ToString());
}
无效client_DownloadFileCompleted(对象发件人,AsyncCompletedEventArgs E)
{
    label2.Text =已完成;
}


解决方案

您应该叫 startDownload()从UI线程。整个构思 WebClient.DownloadFileAsync()是,它会自动生成一个工作线程为你而不会阻塞调用线程。在 startDownload(),您指定的修改我假设由UI线程创建的控件回调。因此,如果你调用 startDownload()从后台线程会导致问题,因为一个线程只能修改它创建用户界面元素。

它应该工作的方式是你叫 startDownload()从UI线程, startDownload()当你定义它建立了由UI线程来处理事件回调。然后,它异步启动下载,并立即返回。 UI线程将被通知当进度变化和code负责更新进度条控件将执行在UI线程上,而且不应该有任何问题。

I am attempting to have a progress bar's progress change as the WebClient download progress changes. This code still downloads the file yet when I call startDownload() the window freezes as it downloads the file. I would like for the user to be able to see the progress change as the splash screen loads. Is there any way to fix this so that the user can see the progress of progressBar2 change?

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";
}

解决方案

You should call startDownload() from the UI thread. The whole idea of WebClient.DownloadFileAsync() is that it will spawn a worker thread for you automatically without blocking the calling thread. In startDownload(), you specified callbacks that modify controls which I assume were created by the UI thread. Thus if you call startDownload() from a background thread it will cause problems, because a thread can only modify UI elements it created.

The way it is supposed to work is you call startDownload() from the UI thread, startDownload() as you defined it sets up event call backs that are handled by the UI thread. It then starts the download asynchronously and returns immediately. The UI thread will be notified when the progress changes and the code responsible for updating the progress bar control will execute on the UI thread, and there shouldn't be any problems.

这篇关于异步文件下载与进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆