简体   繁体   中英

How to show an animated loading form while executing code in Windows Forms C#

I want to display an animated loading form while executing some code in the main form. The animated form is used only to show the user that an operation is executing and I want to close it once the operation finishes. The code that I'm using is:

    public partial class Form_main_admin : Form
    {
        private Thread loadingThread;
        private string loadingText;

        public Form_main_admin()
        {
            InitializeComponent();
        }

     private void main_tabControl_SelectedIndexChanged(object sender, EventArgs e)
     { 
         switch (main_tabControl.SelectedIndex)
         {
             case 0:
                 // ...
                 break;
             case 1:
                 showLoadingForm("Loading");

                 // Load a datagridview (load data, adjust column widths) in Form_main_admin

                 closeLoadingForm();
                 break;
            }
    }

    private void showLoadingForm(string text)
    {
         loadingText = text;
         loadingThread = new Thread(new ThreadStart(openLoadingForm));
         loadingThread.Start();
    }

    private void openLoadingForm()
    {
         try
         {
             Form_loading loadingForm = new Form_loading(loadingText);
             loadingForm.ShowDialog();
         }
         catch 
         {
             Thread.ResetAbort();
         }
     }

     private void closeLoadingForm()
     {
         try
         {
             loadingThread.Abort();
         }
         catch 
         {
             Thread.ResetAbort();
         }

     }
}

The problem is that I get a "Thread was being aborted" exception when I quickly change between tabs (see image in link below).

http://postimg.org/image/bvre2bmi5/

I do not want the user to see this exception if he chages tabs too fast. After reading other posts on this forum I realized that my implementation is not recommended. Could someone please show me how to properly implement this functionality?

If you need an animated progress form, try to use BackgroundWorker class to perform loading in an additional thread:

    public partial class MainForm : Form
    {
        /// <summary>
        /// Some progress form
        /// </summary>
        WaitForm waitForm = new WaitForm();

        /// <summary>
        /// https://msdn.microsoft.com/library/cc221403(v=vs.95).aspx
        /// </summary>
        BackgroundWorker worker = new BackgroundWorker();

        public MainForm()
        {
            InitializeComponent();

            worker.DoWork += (sender, args) => PerformReading();
            worker.RunWorkerCompleted += (sender, args) => ReadingCompleted();
        }

        /// <summary>
        /// This method will be executed in an additional thread
        /// </summary>
        void PerformReading()
        {
            //some long operation here
            Thread.Sleep(5000);
        }

        /// <summary>
        /// This method will be executed in a main thread after BackgroundWorker has finished
        /// </summary>
        void ReadingCompleted()
        {                        
           waitForm.Close();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            //Run reading in an additional thread
            worker.RunWorkerAsync();
            //Show progress form in a main thread
            waitForm.ShowDialog();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM