Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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");
}
Posted
Updated 31-Mar-13 21:05pm
v2
Comments
StM0n 1-Apr-13 3:08am    
You should get rid off Application.DoEvents();
... and what's in count?
Friendsaa 1-Apr-13 3:39am    
count is the files which i am downloading

1 solution

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:
C#
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:
C#
Sleep(500);

Should do it. :luagh:
 
Share this answer
 
Comments
Friendsaa 1-Apr-13 3:48am    
I did not used threading for progressbar as i dont know it
OriginalGriff 1-Apr-13 4:00am    
Oh yes you did! :laugh:

That's what a BackgroundWorker *is* - it's a separate thread that works in the background while you main thread continues with it's work (handling the user interface, hence the "UI Thread" reference above) so that teh user can interact with the display while a long calculation goes on in the background.
If your system has multiple cores, then the Framework / OS will allocate the two jobs to different cores, otherwise the threads will get execution time on the same core depending on their priorities. A BackgroundWorker has a lower priority than the UI thread, so it doesn't interfere with it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900