问题描述
一旦我启动RunWorkerAsync()方法,我就会使用循环将消息发送到UI.这是循环:
Once I fire the RunWorkerAsync() method, I use a loop to send message to the UI. Here is the loop:
int i = 0;
string msg = "Processing";
while (_workerThreadDone == false)
{
if (i > 100)
{
i = 0;
msg = "Processing";
}
msg += " .";
System.Threading.Thread.Sleep(500);
StatusBarVM.Instance.Text1 = msg;
StatusBarVM.Instance.Progress = i++;
System.Windows.Forms.Application.DoEvents();
}
在WorkerCompleted事件中,变量_workerThreadDone设置为true.这种逻辑工作正常.
The variable _workerThreadDone is set to true in WorkerCompleted event. This logic works fine.
但是,在退出我的循环之前,不会触发也会更新MainWindow的ProgressChanged事件.我猜这是因为我的循环阻止了它.所以我需要允许这个ProgressChanged甚至在我的循环循环时被触发 -我该怎么做?
However, the ProgressChanged event which also updates the MainWindow doe not get fired until my loop has exited. I guess, this is because my loop is holding it back. So i need to allow this ProgressChanged even to be fired while my loop is looping - How do I do this?
谢谢.
推荐答案
在循环中添加以下行.
谢谢
Rajnikant
Rajnikant
这篇关于后台工作者线程有疑问吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!