I created a loadingForm with only a progress bar with marquee style. In my mainForm I'm trying to do this:
//before downloading
loadingForm lf = new loadingForm();
lf.Show();
//start downloading
//finishdownloading
lf.Close();
The loadingForm was shown but the progress bar didn't appear, and the form looked like it was crash. after finishing downloading, the lo开发者_运维百科adingForm was closed and my app continued to run normally. In loadingForm I only have:
void loadingForm_Load(object sender, EventArgs e)
{
progressbar1.visible = true;
}
I already set progressbar1 style to marquee in loadingForm.design. How do I fix this? thanks for your help in advance.
You should have a look at using BackgroundWorker Class for the time consuming actions, so that the UI can continue showing the progress while the background worker thread does the work.
- How to: Use a Background Worker
- BackgroundWorker Basics in C#
- C# BackgroundWorker Tutorial
This is most likely because the download and the form with the progress bar are run on the same thread. You could use a BackgroundWorker to perform the download in a different thread than the form.
The UI thread might not have "resources" to redraw the ui, you should use the background worker as mentioned, or process the application messages in the queue. From the Show() method to the Close() method you should be sure to call Application.DoEvents(), so all windows messages will be processed (along with redrawing messages to your application form)
精彩评论