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

效果图

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

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值