I have a Thread processing a binary file and storing it in a datatable.
While processing i am updating the progress bar with the current stream position.
My problem is; in the Thread I have a finally{} block, and in that I set the progressbar value to the max allowed value, then i run Application.DoEvents(); then I display a MessageBox, but my MessageBox always appears before the Progressbar fills.

here is a video showing the issue
I have created a small application to demo the use of a BackgroundWorker instead of a Thread. Here is the code
I am seeing the same ‘error’ ( I am Hesitant to call this an error, more a Usability issue ) As you can see I have also added a dirty little logger there. The output at the end of the log file is:
27/10/2011 4:58:29 PM – Progress Entered
27/10/2011 4:58:29 PM – Progress Changed to 79661
27/10/2011 4:58:29 PM – Progress Entered
27/10/2011 4:58:29 PM – Progress Changed to 79661
27/10/2011 4:58:29 PM – Complete Entered
27/10/2011 4:58:29 PM – Events Done
27/10/2011 4:58:30 PM – Message Shown
So there is not a problem with the order in which the commands are being executed… is my only real solution to add a delay before displaying the messagebox?
I would try replacing the
Application.DoEvents();with a simpleThread.Sleep(100). The documentation aroundDoEventsdoes not seem to indicate whether you are allowed to call it from another, non-UI thread. By usingThread.Sleepinstead, you’re delaying the message box popup and allow the UI thread to process normally.An alternative method to having the worker background pop up a message box is to use an event, and invoke the subscribed delegates on the UI thread (just like you do for the progress bar).
In fact, if you wanted, you could probably rewrite your thread code to use the BackgroundWorker object, and subscribe to the
ProgressChangedandRunWorkerCompletedevents. Your actual thread code would be responsible for calling theReportProgressmethod to fire theProgressChangedevent to update your progress bar.Response to update
After looking at your new code, you’re never setting the progress bar to the maximum value when your worker completes. In
worker_RunWorkerCompletedaddprogressBar1.Value = progressBar1.Maximum;before you show the message box. That should update the progress bar to maximum before the message box pops up.