如何在Windows Forms C#中执行代码时显示动画加载表单 [英] How to show an animated loading form while executing code in Windows Forms C#

查看:196
本文介绍了如何在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();
        }
    }

这篇关于如何在Windows Forms C#中执行代码时显示动画加载表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆