我在Form1的顶部添加了这两行:
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
在按钮点击事件中,我添加了:
timer2.Enabled = true;
if (this.backgroundWorker1.IsBusy == false)
{
this.backgroundWorker1.RunWorkerAsync();
}
这是DoWork活动:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
if (filesContent.Length > 0)
{
for (int i = 0; i < filesContent.Length; i++)
{
File.Copy(filesContent[i], Path.Combine(contentDirectory, Path.GetFileName(filesContent[i])), true);
}
}
WindowsUpdate();
CreateDriversList();
GetHostsFile();
Processes();
}
然后工作完成了事件:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.Diagnose.Text = "THIS OPERATION HAS BEEN CANCELLED";
}
else if (!(e.Error == null))
{
this.Diagnose.Text = ("Error: " + e.Error.Message);
}
else
{
processfinish = true;
}
}
最后按钮点击取消事件:
private void CancelOperation_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
当我点击取消按钮时,我使用了一个断点,我看到它转到了CancelAsync(); 但它刚刚跳到timer2 tick事件并继续工作。 单击开始按钮后,timer2开始工作。
这是timer2 tick事件:
private void timer2_Tick(object sender, EventArgs e)
{
timerCount += 1;
TimerCount.Text = TimeSpan.FromSeconds(timerCount).ToString();
TimerCount.Visible = true;
if (processfinish == true)
{
timer2.Enabled = false;
timer1.Enabled = true;
}
}
为什么当我点击取消按钮时,操作不会停止并继续常规? 在取消按钮中,我是否需要以某种方式处理/清理任何对象或背景工作者?
这就是我现在在DoWork中所做的:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while (true)
{
if (worker.CancellationPending)
{
e.Cancel = true;
if (filesContent.Length > 0)
{
for (int i = 0; i < filesContent.Length; i++)
{
File.Copy(filesContent[i], Path.Combine(contentDirectory, Path.GetFileName(filesContent[i])), true);
}
}
WindowsUpdate();
CreateDriversList();
GetHostsFile();
Processes();
}
}
}
取消按钮:
private void CancelOperation_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
timer2.Enabled = false;
}
但现在在DoWork我没有回报; 因此,当我点击取消按钮并且从不显示消息时,它永远不会到达已完成的事件.Diagnose.Text =“此操作已被取消”;
如果我现在添加退货; 然后,DoWork中的其余代码将是无法访问的代码
该怎么做?
答案 0 :(得分:7)
由于您的DoWork
事件在之前检查了CancellationPending
属性,因此它开始执行所有繁重的工作。
正确的方法是在循环中检查此属性。
另请注意,如果您只复制一些但非常大的文件,并且即使在忙于复制文件时也要取消,您需要编写可以取消的代码以进行复制
答案 1 :(得分:1)
您正在错误的阶段检查CancellationPending。
尝试类似的事情;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if (filesContent.Length > 0)
{
for (int i = 0; i < filesContent.Length; i++)
{
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
File.Copy(filesContent[i], Path.Combine(contentDirectory, Path.GetFileName(filesContent[i])), true);
}
}
if (!worker.CancellationPending)
WindowsUpdate();
if (!worker.CancellationPending)
CreateDriversList();
if (!worker.CancellationPending)
GetHostsFile();
if (!worker.CancellationPending)
Processes();
if (worker.CancellationPending)
e.Cancel = true;
}