BackgroundWorker 組件 -- 進度條
阿新 • • 發佈:2018-08-29
code pla nds chang sender view con tex jpg
代碼:
BackgroundWorker bw = new BackgroundWorker(); public MainWindow() { InitializeComponent(); bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.DoWork += bw_DoWork; bw.ProgressChangedView Code+= bw_ProgressChanged; bw.RunWorkerCompleted += bw_RunWorkerCompleted; } void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.txtMsg.Text = "下載完成"; } void bw_ProgressChanged(objectsender, ProgressChangedEventArgs e) { this.mySlider.Value = (double)e.ProgressPercentage / 10; this.txtMsg.Text = ((double)e.ProgressPercentage / 10).ToString(); } void bw_DoWork(object sender, DoWorkEventArgs e) {for (int i = 1; i < 100; i++) { if (bw.CancellationPending == true) { e.Cancel = true; return; } bw.ReportProgress(i); Thread.Sleep(TimeSpan.FromSeconds(1)); } } private void btnStart_Click(object sender, RoutedEventArgs e) { bw.RunWorkerAsync(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { bw.CancelAsync(); }
BackgroundWorker 組件 -- 進度條