Eric Burton
asked on
URGENT! - VB.NET - Display a running ProgressBar while loading a form
I have a few forms in my application which take a long time to load due to the forms retrieving data from the database. How can I show the user that the forms are indeed working and are not "frozen" as they appear? I would like to use a ProgressBar if possible.
From past reading, I saw that you may have to use different threads to accomplish this. I am a newbie to multi-threading, asynchronous processing so I would appreciate if there are any entry level examples that employ this technique.
Thanks!
From past reading, I saw that you may have to use different threads to accomplish this. I am a newbie to multi-threading, asynchronous processing so I would appreciate if there are any entry level examples that employ this technique.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1. Postpone the time-consuming code until the form has loaded. In the Form_Load, start a Windows Forms Timer with a short interval (50 ms for example). Then, when the timer elapses, stop the timer (avoid that it fires more than once) and do the time-consuming work. It will take just as long, and the form will not be usable during the data-retrieval, but it will be painted nicely before the data-retrieval begins. Maybe this is already good enough.
2. Next, and probably better, is to start a backgroundworker, and do the time-consuming stuff on the background thread. However, you cannot modify any UI element on the background thread. You need to use the "ProgressChanged" or "RunWorkerCompleted" events (that are called on the correct UI thread automatically) for the UI related work.
3. Other Multi-threading options exist, but are probably too complicated for what you need to do.
Also, make suer you do the database-querying as efficient as possible. I don't know how you query your database, but maybe you can avoid a lot of the "long time to load" by optimizing the database access code.