简体   繁体   English

如何在 Windows 窗体 C# 中执行代码时显示动画加载窗体

[英]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/ 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:如果您需要动画进度表,请尝试使用BackgroundWorker类在附加线程中执行加载:

    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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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