C# winform 多线程异步操作线程启动暂停与恢复

        /// <summary>
        /// 线程控制模块
        /// </summary>
        private ManualResetEvent manualResetEvent = new ManualResetEvent(false);
        /// <summary>
        /// 配合使用
        /// </summary>
        private AutoResetEvent autoResetEvent = new AutoResetEvent(true);
        private AsyncOperation asyncOperation = AsyncOperationManager.CreateOperation(null);

        /// <summary>
        /// 存储状态
        /// </summary>
        private bool suspend;

        /// <summary>
        /// 委托操作模块
        /// </summary>
        /// <param name="str"></param>
        private delegate void InvokeDelegate(string str);
        private delegate void InvokeMessage(int result);
        private delegate string[] InvokeMessageResult(int result);
        private delegate void InvokeListView(string oneValue, string twoValue, string threeValue, string four,string five);
        private delegate void InvokeMessageUI(string msg, string value);
        private delegate void InvokeMessageViewUI(string[] value);
        private delegate void InvokeExecute();

        /// <summary>
        /// 
        /// </summary>
        private bool formClosed = false;         

        private void btnDowload_Click(object sender, EventArgs e)
        {
            //启动下载任务
            if (this.btnDowload.Tag == null)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    ExecuteDownload(this.txtPath.Text);
                });
            }
            else if (this.btnDowload.Tag.ToString() == "Run")
            {
                this.suspend = true;
                manualResetEvent.Reset();
                //autoResetEvent.Reset();//另一种方式

                //更改执行按钮状态
                EvenInvokeButtionProcess("继续下载", "Reset");
            }
            else if (this.btnDowload.Tag.ToString() == "Reset")
            {
                //
                this.suspend = false;
                manualResetEvent.Set();
                //autoResetEvent.Set();//另一种方式

                //更改执行按钮状态
                EvenInvokeButtionProcess("暂停下载", "Run");
            }
            /** ----------------------------------------
             * ThreadPool.QueueUserWorkItem(delegate
             *   {
             *       ExecuteDownload(this.txtPath.Text);
             *   });
             * -----------------挂起下载任务------------
             *  this.suspend = true;
             *  manualResetEvent.Reset();
             *  //autoResetEvent.Reset();//另一种方式
             *  -----------------唤醒下载任务-------------
             *  this.suspend = false;
             *  manualResetEvent.Set();
             *  //autoResetEvent.Set();//另一种方式
             ***/
        }

        /// <summary>
        /// 下载档案文件
        /// </summary>
        public void ExecuteDownload(string savePath)
        {
            //EvenInvokeButtionState("btnDowload", "false");
            //更改执行按钮状态
            EvenInvokeButtionProcess("暂停下载", "Run");
            //更改控件展示状态
            EvenInvokeButtionState("btnGet", "false");
            //初始化进度条
            EventInvokeProgresBarInit(0);
            //初始化进度条最大值
            int count = this.listView1.Items.Count;
            EventProgresMaximum(count);
            //this.listView1.Items 的下标从0开始,所以取值时,下标为 Count - 1
            for (int i = this.listView1.Items.Count; i > 0; i--)
            {
                int rowIndex = 0;
                //1.取得一条数据,
                if (i == 0)
                {
                    rowIndex = i;
                }
                else
                {
                    rowIndex = i - 1;
                }
                /**
                * this.listView1.Items 的下标从0开始,所以取值时,下标为 Count - 1,因为 Count 计数器,是从 1开始记录,所以相差1
                * */
                EventListViewSelected(rowIndex);
                //获取列表
                string[] ls = EventListViewGet(rowIndex);
                //更新操作状态
                EventInvokeListBoxText("---------------------------------------------------------", "");
                EventInvokeListBoxText("开始下载:", ls[1].ToString() + "档案文件");
                //更新比例
                EventProgresTipText(count.ToString(), "/" + (rowIndex));
                //执行下载操作
                ExecuteDownload(ls[0].ToString(), ls[1].ToString(), savePath);
                //将比对完成之后的数据移除
                EventListViewRemoveAt(rowIndex);
                //更新进度条状态
                EventInvokeProgresBar(1);
                //---------------------------------------------------------------------------
                if (formClosed)
                {
                    return;
                }
                //监听任务状态
                if (suspend)
                {
                    //更改执行按钮状态
                    EvenInvokeButtionProcess("继续下载", "Reset");
                    //挂起任务
                    this.manualResetEvent.WaitOne();
                }
                /** -------------------------另一种方式-------------------------------------
                 *  if (suspend)
                 *   {
                 *       this.autoResetEvent.WaitOne();
                 *   }
                 *   asyncOperation.Post(delegate { EventMessageBox(str); }, str);
                 **/
            }
            //更新进度条状态
            EventInvokeProgresBar(-100); //-100 为状态位
            //设置按钮状态
            EvenInvokeButtionState("btnGet", "true");
            //EvenInvokeButtionState("btnDowload", "true");
            //更改执行按钮状态
            EvenInvokeButtionProcess("文件下载", null);
            //数据加载完毕,设置提示信息
            EventInvokeListBoxText("---------------------------------------------------------","");
            EventInvokeListBoxText("执行状态:", "档案文件已经下载完毕,等待相关任务执行中.........");
        }

        /// <summary>
        /// 进度提示委托
        /// </summary>
        /// <param name="mes"></param>
        /// <param name="value"></param>
        public void EventInvokeListBoxText(string msg, string value)
        {
            this.Invoke(new InvokeMessageUI(InvokeListBoxText), new object[] { msg, value });
        }

        /// <summary>
        /// 进度提示条改变
        /// </summary>
        /// <param name="mes"></param>
        /// <param name="value"></param>
        public void InvokeListBoxText(string msg, string value)
        {
            if (this.listBox1.Items.Count == 0)
            {
                this.listBox1.Items.Insert(0, msg + value);
            }
            else
            {
                this.listBox1.Items.Insert(this.listBox1.Items.Count -1, msg + value);
            }
            //this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
            this.listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="value"></param>
        private void EvenInvokeButtionProcess(string msg, string value)
        {
            this.Invoke(new InvokeMessageUI(InvokeButtionProcessUI), new object[] { msg, value });
        }

        private void InvokeButtionProcessUI(string msg, string value)
        {
            this.btnDowload.Tag = value;
            this.btnDowload.Text = msg;
        }

 

posted @ 2013-08-14 15:30  如.若  阅读(3148)  评论(0编辑  收藏  举报