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

本文关键字:窗体 代码 显示 加载 动画 执行 Windows | 更新日期: 2023-09-27 18:01:43

我想在主表单中执行一些代码时显示动画加载表单。动画表单仅用于向用户显示正在执行的操作,并且我希望在操作完成后关闭它。我使用的代码是:

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

问题是,当我在标签之间快速切换时,我得到一个"线程被中止"异常(见下面链接中的图像)。

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

我不希望用户看到这个异常,如果他改变选项卡太快。在阅读了这个论坛上的其他帖子后,我意识到我的实现不值得推荐。有人能告诉我如何正确实现这个功能吗?

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

如果你需要一个动画进度表单,尝试使用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();
        }
    }