C# winform 使用进度条(两种形式)

C# winform 使用进度条(两种形式)
2009年02月07日 星期六 15:11

在用c#做WinFrom开发的过程中。我们经常需要用到进度条(ProgressBar)用于显示进度信息。这时候我们可能就需要用到多线程,如果不采用多线程控制进度条,窗口很容易假死(无法适时看到进度信息)。下面我就简单结合一个我写的例子给大家做一个介绍。

第一步:设计界面不说了...注意需要引用 using System.Threading;
第二步:定义一个代理,用于更新ProgressBar的值(Value)

  1.         //更新进度列表
  2.         private delegate void SetPos(int ipos);

第三步:进度条值更新函数(参数必须跟声明的代理参数一样)

  1.         private void SetTextMessage(int ipos)
  2.          {
  3.             if (this.InvokeRequired)
  4.              {
  5.                  SetPos setpos = new SetPos(SetTextMessage);
  6.                 this.Invoke(setpos, new object[] { ipos});
  7.              }
  8.             else
  9.              {
  10.                 this.label1.Text = ipos.ToString() + "/100";
  11.                 this.progressBar1.Value = Convert.ToInt32(ipos);
  12.              }
  13.          }

第四步:函数实现

  1.         private void button1_Click(object sender, EventArgs e)
  2.          {
  3.              Thread fThread = new Thread(new ThreadStart(SleepT));//开辟一个新的线程
  4.              fThread.Start();
  5.          }

第五步:新的线程执行函数:

  1.         private void SleepT()
  2.          {
  3.             for (int i = 0; i < 500; i++)
  4.              {
  5.                  System.Threading.Thread.Sleep(100);//没什么意思,单纯的执行延时
  6.                  SetTextMessage(100 * i / 500);
  7.              }
  8.          }

 

=============================

使用浮动进度条

1.浮动窗口

   ///


        /// Increase process bar
        ///

        /// the value increased
        ///
        public bool Increase(int nValue)
        {

            if (nValue > 0)
            {
                if (prcBar.Value + nValue < prcBar.Maximum)
                {
                    prcBar.Value += nValue;
                    return true;
                }

                else
                {
                    prcBar.Value = prcBar.Maximum;
                    this.Close();
                    return false;
                }

            }
            return false;
        }

 

2.使用进度的窗口

using System.Threading;

     private frmProcessBar myProcessBar = null;
        private delegate bool IncreaseHandle(int nValue);
        private IncreaseHandle myIncrease = null;

 

     private void ShowProcessBar()
        {
            myProcessBar = new frmProcessBar();
            // Init increase event
            myIncrease = new IncreaseHandle(myProcessBar.Increase);
            myProcessBar.StartPosition = FormStartPosition.CenterScreen;
            myProcessBar.ShowDialog();
            myProcessBar = null;

        }
        private void ThreadFun()
        {

            MethodInvoker mi = new MethodInvoker(ShowProcessBar);
            this.BeginInvoke(mi);
            Thread.Sleep(1000);//Sleep a while to show window
            bool blnIncreased = false;
            object objReturn = null;
            do
            {
                Thread.Sleep(50);
                objReturn = this.Invoke(this.myIncrease, new object[] { 2 });
                blnIncreased = (bool)objReturn;
            }
            while (blnIncreased);
        }

 

使用方法:
Thread thdSub = new Thread(new ThreadStart(ThreadFun));
    thdSub.Start();

你可能感兴趣的:(C# winform 使用进度条(两种形式))