如何使用BackgroundWorker?
我知道它有3种方法。 在我的程序中,我有一个方法来发送消息。 这往往是迟到,程序有时根本没有发送消息响应button按下。 有时候我只想到5秒钟,程序就冻结了。 我想使用BackgroundWorker
按预期发送消息,并允许程序在任何时候都能正常运行。 我有在button处理程序中发送消息的代码。 现在我把这个等效的代码放在哪里? 我希望所有这些仍然可以通过button来处理。
这是合适的处理程序?
backgroundWorker1.RunWorkerAsync();
和:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {}
我要把我的代码在button处理程序? 而这之前:
carga.progressBar1.Minimum = 0; carga.progressBar1.Maximum = 100;
Carga是ProgressBar的另一种forms。 在这种情况下如何使用BackgroundWorker?
您只能从ProgressChanged
或RunWorkerCompleted
事件处理程序更新进度栏,因为它们与UI线程同步。
基本的想法是。 Thread.Sleep
只是在这里模拟一些工作。 将其replace为您的真实路由呼叫。
public Form1() { InitializeComponent(); backgroundWorker1.DoWork += backgroundWorker1_DoWork; backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged; backgroundWorker1.WorkerReportsProgress = true; } private void button1_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { for (int i = 0; i < 100; i++) { Thread.Sleep(1000); backgroundWorker1.ReportProgress(i); } } private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; }
我知道这是有点老,但如果其他初学者正在经历这个,我会分享一些代码,涵盖了一些基本的操作,这里是另一个例子,还包括取消过程的选项,也报告给用户进程的状态。 我将在上面的解决scheme中添加由Alex Aza给出的代码的顶部
public Form1() { InitializeComponent(); backgroundWorker1.DoWork += backgroundWorker1_DoWork; backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged; backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted; //Tell the user how the process went backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.WorkerSupportsCancellation = true; //Allow for the process to be cancelled } //Start Process private void button1_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); } //Cancel Process private void button2_Click(object sender, EventArgs e) { //Check if background worker is doing anything and send a cancellation if it is if (backgroundWorker1.IsBusy) { backgroundWorker1.CancelAsync(); } } private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { for (int i = 0; i < 100; i++) { Thread.Sleep(1000); backgroundWorker1.ReportProgress(i); //Check if there is a request to cancel the process if (backgroundWorker1.CancellationPending) { e.Cancel = true; backgroundWorker1.ReportProgress(0); return; } } //If the process exits the loop, ensure that progress is set to 100% //Remember in the loop we set i < 100 so in theory the process will complete at 99% backgroundWorker1.ReportProgress(100); } private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { if (e.Cancelled) { lblStatus.Text = "Process was cancelled"; } else if (e.Error != null) { lblStatus.Text = "There was an error running the process. The thread aborted"; } else { lblStatus.Text = "Process was completed"; } }