多线程异步编程(2):创建多个线程执行任务,同时更新进度条,使用观察者模式,利用事件通知界面更新。移除对Form的引用,彻底解除界面与逻辑的耦合

来源:互联网 发布:淘宝接单app 编辑:程序博客网 时间:2024/06/05 02:52

效果图

1.创建线程参数类和事件参数类

public class ThreadParam
    {
        public int threadType;
        public ThreadParam(int threadType)
        {
            this.threadType = threadType;
        }
    }

    public class ThreadEventArgs : EventArgs
    {
        public double Counter;

        public ThreadEventArgs( double counter)
        {
            this.Counter = counter;
        }
    }

 

2.创建线程要指派的委托和全局锁对象,完成进度

       delegate void CommonBeginInvoke();

       object sync = new object();//所对象

       /// <summary>
        /// 进度
        /// </summary>
        private double Counter;

        /// <summary>
        /// 线程个数
        /// </summary>
        private int Scaler = 0;

       public delegate void EventHandler(object sender,ThreadEventArgs e);

       public event EventHandler CommonNotify;

3..创建线程,实例化线程传递给委托的参数类,通过判断参数类给不同的线程指定相应的方法

public void CreateThread()
        {
ThreadParam threadParam = null;
            for (int key = 0; key < 2; key++)
            {
                threadParam = new ThreadParam(key);
                copyFileThread = new Thread(new ParameterizedThreadStart(Operate));//Operate回调方法
                copyFileThread.Name =key.ToString();
                copyFileThread.IsBackground = true;
                copyFileThread.Start(threadParam);//threadParam线程参数
            }
        }

4.给线程指定委托

void Operate(object param)
        {
            if (param is ThreadParam)
            {
                ThreadParam tParams = param as ThreadParam;
                if (tParams != null)
                {           

                   if (tParams.threadType==1)
                    {
                        CompressFileDelegate copyFileBeginInvoke = FunctionA;

                        copyFileBeginInvoke.BeginInvoke(CallBack, 1);
                    }

                    if (tParams.threadType == 2)
                    {
                        CompressFileDelegate copyFileBeginInvoke = FunctionB;
                        copyFileBeginInvoke.BeginInvoke(CallBack,2);
                    }

                }

            }

       }

5.回调方法

public void CopyFileCallBack(IAsyncResult result)
        {
            object[] tmp = result.AsyncState as object[];
            lock (sync)
            {
                this.Scaler += 1;
                if (this.Scaler == 2)
                    this.Counter = 100;
                else
                    this.Counter = this.Counter + (100 / 2);

                if (this.CommonNotify != null) //判断该事件是否被订阅
                {
                    ThreadEventArgs e = new ThreadEventArgs(tmp[0].ToString(), this.Counter);
                    CommonNotify(this, e);  //给订阅者发送通知
                }
            }
        }

6.Form.cs文件form构造函数中订阅事件

Common common = new Common();
 common.CommonNotify += new Common.EventHandler(UpdateToolStripProgressBarValueEvent);

7.更新界面

public void UpdateToolStripProgressBarValueEvent(object sender,ThreadEventArgse)  //这个方法的样式和委托必须一致
        {
            int value= Convert.ToInt32(e.Counter);
            this.toolStripProgressBar1.Value =value;
            if (value==100)
            {
                MessageBox.Show("success");
                this.toolStripProgressBar1.Value = 0;
                this.toolStripStatusLabel1.Text = "";
                this.buttonCreate.Enabled = true;
            }
        }

 

 

 

 

 

 

原创粉丝点击