Displaying Progress Bar on Windows Forms

pizzaboy

Well-known member
Joined
Jan 5, 2008
Messages
57
Programming Experience
1-3
I have a progress bar on a Windows Form which displays the number of records as they are retrieved from the database.

The problem I'm having is that the entire Form.Load() method seems to run prior to the form being displayed, which means all my code updating the progress bar is gone to waste.

I presume this is something to do with threading?


Any ideas would be greatly appreciated.

Thanks.
 
It's nothing to do with threading. That's how the Load event works: the Load event handler completes, then the form is displayed. If you want to do something AFTER the form is displayed then you handle the Shown event.
 
Thanks for that.

Managed to get the progress bar working. The problem I'm having was that I also wanted a label to be updated to inform the user how many records are being processed ie. 10 of 20150.

I can't appear to get the label to update. During the Form.Shown event, the entire form shows only a partially rendered form.

Any ideas?
 
That would presumably be because you are processing the data on the UI thread. The thread can't update the UI if it's busy processing the data. The poor man's option would be to call Application.DoEvents intermittently to allow the UI to update. The more professional option would be to do the processing on a secondary thread, e.g. using a BackgroundWorker, so the UI thread remains responsive.
 
Back
Top