C#多线程进度条

 public class ZyjProgressBar : System.Windows.Forms.ProgressBar
    {
        //用于跨线程访问控件的委托
        private delegate void deleByControl(int v);

        //用于执行的任务
        public Action Task { get; set; }
        private Thread taskThread;
        private deleByControl setValueDele;


        public ZyjProgressBar()
        {
            //初始化线程
            this.taskThread = new Thread(StartTask);
            this.taskThread.IsBackground = true;
            //初始化委托
            this.setValueDele = new deleByControl(AddValue);

        }

        private void StartTask() {
            this.Task();
        }

        /// <summary>
        /// 增加进度条的进度
        /// </summary>
        public void AddValue(int value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(this.setValueDele, 3);
            }
            else
            {
                this.Value += value;
            }
        }
    }

 

posted @ 2014-08-04 23:33  yzeng  阅读(1493)  评论(0编辑  收藏  举报