C#學習-BackgroundWorker控制項和ProgressBar控制項使用

來源:互聯網
上載者:User

1建立BackgroundWorker控制項
建立Windows表單應用程式,在空白Form1下添加BackgroundWorker控制項,從工具箱中拖動即可。
我們需要使用BackgroundWorker的幾個事件:
DoWork事件:在調用RunWorkerAsync函數時觸發事件,實現背景程式調用。
ProgressChanged:在調用ReportProgress函數時觸發事件,實現背景程式的進度跟蹤。在使用ReportProgress報告進度更新時,WorkerReportsProgress屬性應為true,否則會出現異常。
RunWorkerCompleted:背景程式完成、異常或者取消時觸發。

2建立ProgressBar進度條控制項
progressBar1.Value的值是從0到100,使用時要注意百分比。

3建立TextBow控制項
用來顯示背景程式進度更新資訊。


控制項代碼:

//             // backgroundWorker1            //             this.backgroundWorker1.WorkerReportsProgress = true;            this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);            this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);            this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);            //             // progressBar1            //             this.progressBar1.Location = new System.Drawing.Point(12, 12);            this.progressBar1.Name = "progressBar1";            this.progressBar1.Size = new System.Drawing.Size(428, 20);            this.progressBar1.TabIndex = 1;            this.progressBar1.Click += new System.EventHandler(this.progressBar1_Click);            //             // textBox1            //             this.textBox1.Location = new System.Drawing.Point(12, 38);            this.textBox1.Multiline = true;            this.textBox1.Name = "textBox1";            this.textBox1.Size = new System.Drawing.Size(428, 53);            this.textBox1.TabIndex = 2;            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

事件代碼:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)        {            for (int i = 0; i < 1000 ;i++ )            {                System.Threading.Thread.Sleep(10);                this.backgroundWorker1.ReportProgress(i / 10, i);            }        }        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)        {            this.progressBar1.Value = e.ProgressPercentage;            textBox1.Text = e.UserState.ToString();        }        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)        {            this.progressBar1.Value = 100;        }        private void progressBar1_Click(object sender, EventArgs e)        {        }        private void textBox1_TextChanged(object sender, EventArgs e)        {        }        private void button2_Click(object sender, EventArgs e)        {            backgroundWorker1.RunWorkerAsync();        }

通過點擊按鈕,調用backgroundWorker1.RunWorkerAsync(),觸發backgroundWorker1_DoWork事件,這裡用System.Threading.Thread.Sleep(10)進行了系統睡眠,ReportProgress(i / 10, i)報告了100次更新。觸發backgroundWorker1_ProgressChanged事件,得到進度和進度資訊,分別顯示到progressBar和textBox中。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.