How do I show a “Loading . . . please wait” message in Winforms for a long loading form?

前端 未结 12 618
一生所求
一生所求 2020-12-04 17:29

I have a form that is very slow because there are many controls placed on the form.

As a result the form takes a long time to loaded.

How do I load the fo

12条回答
  •  抹茶落季
    2020-12-04 18:09

    Using a separate thread to display a simple please wait message is overkill especially if you don't have much experience with threading.

    A much simpler approach is to create a "Please wait" form and display it as a mode-less window just before the slow loading form. Once the main form has finished loading, hide the please wait form.

    In this way you are using just the one main UI thread to firstly display the please wait form and then load your main form.

    The only limitation to this approach is that your please wait form cannot be animated (such as a animated GIF) because the thread is busy loading your main form.

    PleaseWaitForm pleaseWait=new PleaseWaitForm ();
    
    // Display form modelessly
    pleaseWait.Show();
    
    //  ALlow main UI thread to properly display please wait form.
    Application.DoEvents();
    
    // Show or load the main form.
    mainForm.ShowDialog();
    

提交回复
热议问题