BackgroundWorker 组件 -- 进度条
Posted sportdog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BackgroundWorker 组件 -- 进度条相关的知识,希望对你有一定的参考价值。
代码:
BackgroundWorker bw = new BackgroundWorker(); public MainWindow() { InitializeComponent(); bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.DoWork += bw_DoWork; bw.ProgressChanged += bw_ProgressChanged; bw.RunWorkerCompleted += bw_RunWorkerCompleted; } void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.txtMsg.Text = "下载完成"; } void bw_ProgressChanged(object sender, 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 组件 -- 进度条的主要内容,如果未能解决你的问题,请参考以下文章