2010/4/10 9:12:16

网站建设技术应用:winform 异步程序进度条的实现

网站建设技术应用:winform 异步程序进度条的实现

  这里用到的winform进度条控件,我是从网上找的,感觉控件总体上来说很美观,下载地址忘了在哪里了,所以各位看官可以去网上找一款心仪的进度条控件,好,废话就说到这里,我们开始了我们今天的任务。
第一步:把进度条控件拖拽到我们的form窗口中,设置下控件的属性,如:Step(步长),Maxinum(最大值),Mininum(最小值),MainColor(主颜色)
 
第二步:双击我们要开始运行的Button按钮
 
        private void btnTongBu_Click(object sender, EventArgs e)
        {
            new System.Threading.Thread(new System.Threading.ThreadStart(StartProgress)).Start();
        }
在线程中开始执行我们需要开始显示进度条的工作任务。
 
        public void StartProgress()
        {
            ProgressStart progress= new ProgressStart (this);
            progress.onProgress += new ProgressStart .dProgress(dProgress_onProgressStart);
            progress.Start();
        }
 
       

 //更新UI
        void dProgress_onProgressStart(long total, long current)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ProgressStart .dProgress(dProgress_onProgressStart), new object[] { total, current });
            }
            else
            {
                this.userControl11.Maximum = (int)total;
                this.userControl11.Value = (int)current;
            }
        }

        /// <summary>
        /// 处理类
        /// </summary>
        public class ProgressStart
        {
            ProductConvertInventory F = null;
            public ProgressStart ( ProductConvertInventory _F)
            {
                F = _F;
            }
            //委托
            public delegate void dProgress(long total, long current);
            //事件
            public event dProgress onProgress;
            //开始模拟工作
            public void Start()
            {
               if(F!=null)
               {
                   if (this.F.lblInventionID.Text != "")
                   {
                       if (this.F.lblComputationID.Text != "")
                       {
                           RM.BLL.db_product productBll = new RM.BLL.db_product();
                           DataTable dt = productBll.GetList();
                           int strNums = 0;
                           if (dt.Rows.Count > 0)
                           {
                               for (int i = 0; i < dt.Rows.Count; i++)
                               {
                                         
                                 int saa = ((i + 1) * 100 / dt.Rows.Count);
                                   if (onProgress!= null)
                                       onProgress(100, saa);
                                   System.Threading.Thread.Sleep(30);                               }
               }
            }
        }