BackgroundWorker 帶進度條簡單用法
阿新 • • 發佈:2019-02-14
namespace WinFormReport
{
public partial class MainForm : Form
{
private BackgroundWorker worker = new BackgroundWorker();
public MainForm()
{
InitializeComponent();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
//正式做事情的地方
worker.DoWork += new DoWorkEventHandler(DoWork);
//任務完稱時要做的,比如提示等等
worker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged);
//任務進行時,報告進度
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork);
}
//呼叫 RunWorkerAsync 時發生
public void DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ComputeFibonacci(worker, e);//當ComputeFibonacci(worker, e)返回時,非同步過程結束
}
//呼叫 ReportProgress 時發生
public void ProgessChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
//當後臺操作已完成、被取消或引發異常時發生
public void CompleteWork(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("完成!");
}
private int ComputeFibonacci(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 1000; i++)
{
if (worker.CancellationPending)
{
e.Cancel = true;
return -1;
}
else
{
int percent = 100 * i / 1000;//計算已完成的百分比
worker.ReportProgress(percent);
}
System.Threading.Thread.Sleep(10);
}
return -1;
}
private void btnStart_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
btnStart.Enabled = false;
btnPause.Enabled = true;
}
private void btnPause_Click(object sender, EventArgs e)
{
btnPause.Enabled = false;
btnStart.Enabled = true;
worker.CancelAsync();
}
}
}
{
public partial class MainForm : Form
{
private BackgroundWorker worker = new BackgroundWorker();
public MainForm()
{
InitializeComponent();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
//正式做事情的地方
worker.DoWork += new DoWorkEventHandler(DoWork);
//任務完稱時要做的,比如提示等等
worker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged);
//任務進行時,報告進度
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork);
}
//呼叫 RunWorkerAsync 時發生
public void DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ComputeFibonacci(worker, e);//當ComputeFibonacci(worker, e)返回時,非同步過程結束
}
//呼叫 ReportProgress 時發生
public void ProgessChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
//當後臺操作已完成、被取消或引發異常時發生
public void CompleteWork(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("完成!");
}
private int ComputeFibonacci(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 1000; i++)
{
if (worker.CancellationPending)
{
e.Cancel = true;
return -1;
}
else
{
int percent = 100 * i / 1000;//計算已完成的百分比
worker.ReportProgress(percent);
}
System.Threading.Thread.Sleep(10);
}
return -1;
}
private void btnStart_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
btnStart.Enabled = false;
btnPause.Enabled = true;
}
private void btnPause_Click(object sender, EventArgs e)
{
btnPause.Enabled = false;
btnStart.Enabled = true;
worker.CancelAsync();
}
}
}