这就是我在我的代码做的: 在BackgroundWorker的DoWork的事件中,我所做的:如何暂停和恢复BackgroundWorker?
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;
}
什么都没有发生,只有当我点击恢复按钮的过程开始,然后当我点击暂停按钮什么都没有发生。
我希望当我点击启动过程按钮时,它会定期启动背景工作,然后单击暂停按钮时它将暂停,恢复按钮将恢复。
我做错了什么?有人可以修复我的代码?
打开的问题编辑... aaaand放弃:) – Reniuz
只需修改工人可以访问的布尔值?我会在backgroundWorker中放弃这一点,而不是暂停线程本身。 – Aphelion
Aphelion请给我一个例子谢谢。 –