更新后台工作者的标签内容 [英] Update Label Content in background worker

查看:65
本文介绍了更新后台工作者的标签内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在后台工作程序中运行的进程。

i需要实现另一个DownloadProgressChangedEventHandler用于下载文件的进程并需要在UI上显示进度百分比。完整代码在类文件中



代码示例



I have a Process running in background worker .
i need to implement another DownloadProgressChangedEventHandler for Downloading Process of the file and need to show Progress in percentage on UI.Complete Code is in Class file

Code sample

public void _backgroundWorkerDoWork(object sender, DoWorkEventArgs e)
       {





一些代码......

_backgroundWorker.ReportProgress(1);



一些代码......

_backgroundWorker.ReportProgress(10);





Some Code ......
_backgroundWorker.ReportProgress(1);

Some Code ......
_backgroundWorker.ReportProgress(10);

_wbClient = new WebClient();
             _wbClient.DownloadFile(SeverPath +FileName, DownLoadedFilePath +FileName);









}







需要显示使用





}



Need to show download Progreess of Webclient using

webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
              webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);




$ b GUI上$ b





请帮助..




on GUI

Please help..
.

推荐答案

BackgroundWorker类 [ ^ ]提供了一种方法ReportProgress [ ^ ]导致 ProgressChanged [ ^ ]事件不是在工作线程中而是在创建BackgroundWorker的线程中。通常那是GUI线程。因此,您可以在工作线程中调用该方法并在GUI线程中处理该事件,这可以毫不费力地更改标签。





现在你必须重命名

The BackgroundWorker class[^] offers a method ReportProgress[^] that causes the ProgressChanged[^] event to be risen not in the working thread but in the thread that created the BackgroundWorker. Usually that is the GUI thread. You can therefore call the method in the worker thread and handle the event in the GUI thread, which can change a label without hassle.


Now you have to rename
new DownloadProgressChangedEventHandler(ProgressChanged)



to

new DownlaodProgressChangedEventHandler(DownloadProgressChanged)



所以方法名称不会干扰其他 ProgressChanged 方法。



然后实现


so the method name doesn''t interfere with the "other" ProgressChanged method.

Then implement

public void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    DownloadProgressBar.Value = e.ProgressPercentage;
}



[/编辑]


[/Edit]


这篇关于更新后台工作者的标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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