2013年9月6日 星期五

【C#】Using Progress Bar 使用ProgressBar顯示進度

如何在背景工作及時回報Loading Bar

使用backgroundWorker後要開啟下面三個Event就可以完成我們想要做的事。


public Main() { InitializeComponent(); backgroundWorker.RunWorkerAsync(); backgroundWorker.WorkerReportsProgress = true;//啟動回報進度 pgbShow.Maximum = 1000;//ProgressBar上限 pgbShow.Minimum = 0;//ProgressBar下限 } private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 1000; i++) { System.Threading.Thread.Sleep(10); backgroundWorker.ReportProgress(i); } } private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { pgbShow.Value = e.ProgressPercentage; //當backgroundWorker的i改變時就會觸發,進而更改pgbShow.Value } private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { MessageBox.Show("Processing was completed"); //當backgroundWorker工作完成時顯示 }

2 則留言: