C# how to load a form with marquee progress bar?

六眼飞鱼酱① 提交于 2019-12-12 13:28:35

问题


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 loadingForm 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.


回答1:


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



回答2:


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.




回答3:


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)



来源:https://stackoverflow.com/questions/4499561/c-sharp-how-to-load-a-form-with-marquee-progress-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!