这就是我在代码中的做法: 在 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,然后当单击暂停按钮时,它将暂停,而恢复按钮将恢复。
我做错了什么 ?有人可以修复我的代码吗?