簡單的c# 進度條的使用

來源:互聯網
上載者:User

在用c#做WinFrom開發的過程中。我們經常需要用到進度條(ProgressBar)用於顯示進度資訊。這時候我們可能就需要用到多線程,如果不採用多線程式控制制進度條,視窗很容易假死(無法適時看到進度資訊)。下面我就簡單結合一個我寫的例子給大家做一個介紹。

第一步:設計介面不說了...注意需要引用 using System.Threading;
第二步:定義一個代理,用於更新ProgressBar的值(Value)

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

 

  1. 第三步:進度條值更新函數(參數必須跟聲明的代理參數一樣)
  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.         }

到此一個簡單的進度條程式做好了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.