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

前端 未结 12 589
一生所求
一生所求 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:22

    You should create a background thread to to create and populate the form. This will allow your foreground thread to show the loading message.

    0 讨论(0)
  • 2020-12-04 18:22

    You can take a look at my splash screen implementation: C# winforms startup (Splash) form not hiding

    0 讨论(0)
  • 2020-12-04 18:23

    I put some animated gif in a form called FormWait and then I called it as:

    // show the form
    new Thread(() => new FormWait().ShowDialog()).Start();
    
    // do the heavy stuff here
    
    // get the form reference back and close it
    FormWait f = new FormWait();
    f = (FormWait)Application.OpenForms["FormWait"];
    f.Close();
    
    0 讨论(0)
  • 2020-12-04 18:23

    Well i do it some thing like this.

            NormalWaitDialog/*your wait form*/ _frmWaitDialog = null;
    
    
            //Btn Load Click Event
            _frmWaitDialog = new NormalWaitDialog();
            _frmWaitDialog.Shown += async (s, ee) =>
            {
                await Task.Run(() =>
               {
                   // DO YOUR STUFF HERE 
    
                   //Made long running loop to imitate lengthy process
                   int x = 0;
                   for (int i = 0; i < int.MaxValue; i++)
                   {
                       x += i;
                   }
    
               }).ConfigureAwait(true);
                _frmWaitDialog.Close();
            };
            _frmWaitDialog.ShowDialog(this);
    
    0 讨论(0)
  • 2020-12-04 18:25

    You want to look into 'Splash' Screens.

    Display another 'Splash' form and wait until the processing is done.

    Here is an example on how to do it.

    0 讨论(0)
  • 2020-12-04 18:32

    Another way of making "Loading screen" only display at certain time is, put it before the event and dismiss it after event finished doing it's job.

    For example: you want to display a loading form for an event of saving result as MS Excel file and dismiss it after finished processing, do as follows:

    LoadingWindow loadingWindow = new LoadingWindow();
    
    try
    {
        loadingWindow.Show();                
        this.exportToExcelfile();
        loadingWindow.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception EXPORT: " + ex.Message);
    }
    

    Or you can put loadingWindow.Close() inside finally block.

    0 讨论(0)
提交回复
热议问题