gif动画进度提示的多线程窗体

当从数据库获取单线图数据时,需一段时间等待,故在等待提示窗口中使用gif动画来表示操作正在运行中,但当笔者将等待提示窗口放在main()主线程中时,发现gif动画不会动了

解决方法:带gif动画的等待提示窗口在另一个线程中运行

下面是等待提示窗口的代码

public partial class Form_wait_thread : Form
    {
        public Form_wait_thread()
        {
            InitializeComponent();
        }
        private delegate void SetTextHandler(string text);
        public void SetText(string text)
        {
            text =text.Replace("\n","");
            text = text.Replace("\r", "");
            if (this.label_txt.InvokeRequired)
            {
                this.Invoke(new SetTextHandler(SetText), text);
            }
            else
            {
                //int t_txtW = text.Length * 14;
                this.label_temp.Text = text;
                this.label_txt.Text = text;
                this.label_txt.Width = this.label_temp.Width;
            }
        }
        private delegate void SetFormWidthHandler(string text);
        public void SetFormWidth(string text)
        {
            text = text.Replace("\n", "");
            text = text.Replace("\r", "");
            if (this.InvokeRequired)
            {
                this.Invoke(new SetFormWidthHandler(SetFormWidth), text);
            }
            else
            {
                //int t_txtW = text.Length * 14;
                this.Width = this.label_temp.Width + this.label_pic.Width;
                this.Refresh();
            }
        }
    }

下面的代码将上面的窗口类放入多线程中

class Form_wait_service
    {
        public static void CreateWaitForm()
        {
            Form_wait_service.Instance.CreateForm();
        }

        public static void CloseWaitForm()
        {
            Form_wait_service.Instance.CloseForm();
        }

        public static void SetWaitFormDispMsg(string text)
        {
            Form_wait_service.Instance.SetDispMsg(text);
        }

        private static Form_wait_service _instance;
        private static readonly Object syncLock = new Object();

        public static Form_wait_service Instance
        {
            get
            {
                if (Form_wait_service._instance == null)
                {
                    lock (syncLock)
                    {
                        if (Form_wait_service._instance == null)
                        {
                            Form_wait_service._instance = new Form_wait_service();
                        }
                    }
                }
                return Form_wait_service._instance;
            }
        }

        private Form_wait_service()
        {
        }

        private Thread waitThread;
        private Form_wait_thread waitForm;

        private void CreateForm()
        {
            if (waitThread != null)
            {
                try
                {
                    waitThread.Abort();
                }
                catch (Exception)
                {
                }
            }

            waitThread = new Thread(new ThreadStart(delegate()
            {
                waitForm = new Form_wait_thread();
                Application.Run(waitForm);
            }));
            waitThread.Start();
        }

        private void CloseForm()
        {
            if (waitThread != null)
            {
                try
                {
                    waitThread.Abort();
                }
                catch (Exception)
                {
                }
            }
        }

        private void SetDispMsg(string text)
        {
            if (waitForm != null)
            {
                try
                {
                    waitForm.SetText(text);
                    waitForm.SetFormWidth(text);
                }
                catch (Exception)
                {
                }
            }
        }

    }

下面是多线程进度提示窗体调用代码

Form_wait_service.SetWaitFormDispMsg("显示的信息");

所有代码打包下载:gif动画进度提示的多线程窗体

 

 

posted @ 2012-07-16 18:50  simplefrog  阅读(1481)  评论(0编辑  收藏  举报