日期:2014-05-18 浏览次数:21275 次
private void btnGo_Click(object sender, EventArgs e) { if (btnGo.Text=="执行") { this.Text = "执行中"; btnGo.Text = "停止"; System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(EmulateLongProcess)); t.Start(); } else { isCanceled = true; this.Text = "已经停止"; btnGo.Text = "执行"; } } private void EmulateLongProcess() { isCanceled = false; for (int i = 1; i <= 500000;i++ ) { if (isCanceled==true) { break; } //System.Threading.Thread.Sleep(10); int j = System.Threading.Thread.CurrentThread.ManagedThreadId; double precentDone = (Convert.ToDouble(i) / 500000) * 100; UpdateProgress(precentDone); } } private delegate void UpdateProgressDelegate(double precentDone); private void UpdateProgress(double precentDone) { if (InvokeRequired==true) { BeginInvoke(new UpdateProgressDelegate(UpdateProgress), new object[] { precentDone }); return; } if (precentDone==0) { isCanceled = true; } if (isCanceled) { progressBar.Value = 0; } else { progressBar.Value = Convert.ToInt16(precentDone); } statuslable.Text = progressBar.Value.ToString() + "%完成"; if (precentDone==0) { statuslable.Text = string.Empty; } }