如何暂停和恢复BackgroundWorker?

这就是我在我的代码中做到的方式:在backgroundworker dowork事件中我做了:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { _busy.WaitOne(); this.Invoke(new MethodInvoker(delegate { label2.Text = "Website To Crawl: "; })); this.Invoke(new MethodInvoker(delegate { label4.Text = mainUrl; })); webCrawler(mainUrl, levelsToCrawl, e); } 

然后在暂停按钮单击事件我做了:

 private void button4_Click(object sender, EventArgs e) { _busy.Reset(); } 

在简历按钮单击事件我做了:

 private void button5_Click(object sender, EventArgs e) { _busy.Set(); } 

但是当我点击开始这个过程时它不起作用:

 private void button1_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); button1.Enabled = false; this.Text = "Processing..."; label6.Text = "Processing..."; label6.Visible = true; button2.Enabled = false; checkBox1.Enabled = false; checkBox2.Enabled = false; numericUpDown1.Enabled = false; button3.Enabled = true; } 

只有当我点击恢复按钮时才会发生任何事情,然后当我点击暂停按钮时没有任何反应。

我希望当我按下启动过程按钮时,它会定期启动后台工作,然后当点击暂停按钮时它将暂停,恢复按钮将恢复。

我做错了什么 ? 有人可以修复我的代码吗?

在BackgroundWorker线程代码中,您需要找到可以安全“暂停”执行的位置。 ManualResetEvent是正确的代码方式。 这篇文章可能有所帮助: 有没有办法无限期地暂停一个post?

基本上,在您希望允许其暂停的工作线程代码中的几个选择点中,尝试插入:

 _busy.WaitOne(Timeout.Infinite); 

当你想暂停(从你的主线程)你使用:

 _busy.Reset(); 

并恢复:

 _busy.Set(); 

你应该可以使用像这样的ManualResetEvent来做到这一点……

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { _busy.WaitOne(); test(mainUrl, levelsToCrawl, e); } 

…然后当你想暂停线程调用_busy.Reset() …当你想重新启动它时,调用_busy.Set()

另外,你可以放置_busy.WaitOne(); 你想要暂停的任何地方。

我一直在寻找这个线程的答案,但我想出了我自己的解决方案,我只是想和你分享。 希望这有效。

我有一个后台工作者,当我点击我的表格的关闭按钮时我想暂停它。 问“你即将取消这个过程”,所以它应该暂停这个过程。

声明bool pauseWorker = false; 在你的class上

 private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { while (condition) { if (pauseWorker == true) { while (pauseWorker == true) { if (pauseWorker == false) break; } } else { //continue process... your code here } } } private void frmCmsnDownload_FormClosing(object sender, FormClosingEventArgs e) { if (bgWorker.IsBusy) { pauseWorker = true; //this will trigger the dowork event to loop that //checks if pauseWorker is set to false DiaglogResult x = MessageBox.Show("You are about cancel the process", "Close", MessageBoxButtons.YesNo); if (x == DialogResult.Yes) bgWorker.CancelAsync(); else { e.Cancel = true; pauseWorker = false; //if the user click no //the do work will continue the process return; } } } 

因此,这里的主要解决方案是控制BGWorker的DoWork事件的布尔声明。 希望此解决方案可以帮助您解决 谢谢。

这对我有用:

 bool work = true; backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.DoWork += backgroundWorker1_DoWork; backgroundWorker1.ProgressChanged += myChangeFunction; backgroundWorker1.RunWorkerAsync(); private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while (true && work) { // Your code here backgroundWorker1.ReportProgress(0); Thread.Sleep(1000); } e.Cancel = true; } private void myChangeFunction(object sender, ProgressChangedEventArgs e) { // Here you can change label.text or whatever thing the interface needs to change. } private void Stop() { work = false; } private void Start() { work = true; backgroundWorker1.RunWorkerAsync(); } 

注意:如果要更改界面的某些内容,则必须将其放在myChangeFunction()中,因为在DoWork()函数中将无法正常工作。 希望这可以帮助。