MessageBox在进度条完成其工作之前显示消息.... [英] MessageBox is displaying message before progressbar finishes its work....

查看:114
本文介绍了MessageBox在进度条完成其工作之前显示消息....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是在进度条完成之前显示Messagebox。看下面的代码



 private void Form1_Load(object sender,EventArgs e)
{
bgw.RunWorkerAsync() ;
}

private void bgw_DoWork(object sender,DoWorkEventArgs e)
{
for(int i = 0; i< = count; i ++)
{
bgw.ReportProgress((i * 100)/ count);
}
}

private void bgw_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

private void bgw_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
Application.DoEvents();
MessageBox.Show(Succcesfully Updated);
}

解决方案

没有图片会很难,我怀疑...

BackgroundWorker在与UI不同的线程上运行,并且它很可能也可以在不同的核心上运行,因此它可以在UI运行时运行和运行。

所以让我们看一下BGW生成的事件序列:

进度(98%)
进度(99%)
进度(100%)
WorkerFinished



这些事件导致在UI线程中生成一个事件,该线程响应:在Progress事件的情况下,它设置进入Progress控件的新值,它会导致UI线程上的另一个事件 - 由于更新而在控件上执行Invalidate。无论如何,Paint是一个低优先级的事件,并且不会立即发布,所以如果工作人员在绘制动作之前完成,BGW已经将WorkerFinished事件放入UI队列中并且接下来执行它,然后在Paint之前执行甚至排队,更少执行!



由于MessageBox是模态控件,很可能直到屏幕进度的实际更新才会完成在用户按下确定按钮后!



如果你想让它完全按照你想要的那样做,那么你需要暂停BGW在终止帖子之前允许进度更新完成的时刻:

睡眠( 500 ); 



应该这样做。 :luagh:


My problem is Messagebox is displaying before progress bar finished. see the below code

private void Form1_Load(object sender, EventArgs e)
{
    bgw.RunWorkerAsync();
}

private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 0; i <= count; i++)
    {
        bgw.ReportProgress((i * 100) / count);
    }
}

private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    Application.DoEvents();
    MessageBox.Show("Succcesfully Updated");
}

解决方案

This is going to be difficult without pictures, I suspect...
The BackgroundWorker operates on a different thread to the UI, and there is a good chance that it can operate on a different core as well, so it can operate and run while your UI is operating.
So let''s look at the sequence of events generated by the BGW:

Progress(98%)
Progress(99%)
Progress(100%)
WorkerFinished


These events cause an Event to be generated in the UI thread, which responds: in the case of the Progress Event, it sets a new value into the Progress control and that causes another event on the UI thread - a Paint due to the update doing an Invalidate on the control. Paint is a low priority event anyway, and won''t be issued immediately, so if the worker finishes before the paint is actioned, the BGW has already placed the WorkerFinished Event into the UI queue and it gets executed next, before the Paint is even queued, much less executed!

Since a MessageBox is a modal control, it is quite possible that the actual update of the on-screen progress will not be done until after the user has pressed the "OK" button!

If you want to get this to do exactly what you want, then you need to "pause" the BGW for a moment to allow the Progress updates to finish before you terminate the thread:

Sleep(500);


Should do it. :luagh:


这篇关于MessageBox在进度条完成其工作之前显示消息....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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