我调用了CancelAsync,但是在我的DoWork functio中,我不知道在什么地方检查取消事件的位置--在这么多需要有序执行的函数中,不管现在是什么函数,它都要停止:
private void bwAsync_DoWork(object sender, DoWorkEventArgs e)
{
// The sender is the BackgroundWorker object we need it to
// report progress and check for cancellation.
BackgroundWorker bwAsync = sender as BackgroundWorker;
while (bwAsync.CancellationPending == false)
{
//Criar o Backup dos ficheiros do cliente e do serviço antigo
UpdateMessage("A criar backup dos ficheiros de Cliente");
Program.Funcoes.DirectoryCopy(Global.OldClient, textBoxBackup.Text + "\\OrcaClientBackup", true, true);
bwAsync.ReportProgress(10);
UpdateMessage("A criar backup dos ficheiros do Serviço");
Program.Funcoes.DirectoryCopy(Global.OldService, textBoxBackup.Text + "\\OrcaServiceBackup", true, true);
bwAsync.ReportProgress(15);
////A Parar e desinstalar o Serviço Actual
UpdateMessage("A parar o Serviço existente");
Program.Funcoes.StopService("OrcaService", 1000, true);
bwAsync.ReportProgress(20);
//Eliminar os ficheiros do Serviço Antigo
UpdateMessage("A preparar os ficheiros para actualizar o cliente");
//Program.Funcoes.DeleteAll(Global.OldService);
bwAsync.ReportProgress(30);
//Eliminar os ficheiros do Serviço Antigo
//Program.Funcoes.DeleteAll(Global.OldClient);
UpdateMessage("A preparar os ficheiros para actualizar o serviço");
bwAsync.ReportProgress(40);
//Copiar os Novos Ficheiros
UpdateMessage("A actualizar o Cliente");
Program.Funcoes.DirectoryCopy(Global.NovoCliente, Global.OldClient, true, false);
bwAsync.ReportProgress(50);
UpdateMessage("A actualizar o Serviço");
Program.Funcoes.DirectoryCopy(Global.NovoServiço, Global.OldService, true, false);
bwAsync.ReportProgress(55);
//Fazer as alterações ao Orca.config.exe
UpdateMessage("A configurar o Cliente");
WriteConfigOrca(Global.OldClient);
Program.Funcoes.UpdateCliente(Global.OldClient + @"\Orca.exe.config", Global.NovoCliente + @"\Orca.exe.config");
bwAsync.ReportProgress(70);
UpdateMessage("A configurar o Serviço");
Program.Funcoes.UpdateService(Global.OldService + @"\OrcaService.exe.config", Global.NovoServiço + @"\OrcaService.exe.config");
//WriteConfigOrcaService(Program.OldService + "\\OrcaService.exe.config");
bwAsync.ReportProgress(85);
//Instalar o serviço
UpdateMessage("A instalar o novo Serviço");
Program.Funcoes.InstallService(Global.OldService + "\\OrcaService.exe");
Thread.Sleep(100);
bwAsync.ReportProgress(100);
return;
}
}我需要在哪里查一下那个cancellationPending?
发布于 2016-04-19 14:41:09
CancelAsync()方法只会在工作线程上将bwAsync.CancellationPending标志设置为true。为了使取消操作实际进行,每当您认为进程准备被取消时,您必须验证该标志,如下所示:
private void bwAsync_DoWork(object sender, DoWorkEventArgs e)
{
// The sender is the BackgroundWorker object we need it to
// report progress and check for cancellation.
BackgroundWorker bwAsync = sender as BackgroundWorker;
if(!bwAsync.CancellationPending)
{
//Criar o Backup dos ficheiros do cliente e do serviço antigo
UpdateMessage("A criar backup dos ficheiros de Cliente");
Program.Funcoes.DirectoryCopy(Global.OldClient, textBoxBackup.Text + "\\OrcaClientBackup", true, true);
bwAsync.ReportProgress(10);
}
if(!bwAsync.CancellationPending)
{
UpdateMessage("A criar backup dos ficheiros do Serviço");
Program.Funcoes.DirectoryCopy(Global.OldService, textBoxBackup.Text + "\\OrcaServiceBackup", true, true);
bwAsync.ReportProgress(15);
}
if(!bwAsync.CancellationPending)
{
////A Parar e desinstalar o Serviço Actual
UpdateMessage("A parar o Serviço existente");
Program.Funcoes.StopService("OrcaService", 1000, true);
bwAsync.ReportProgress(20);
}
if(!bwAsync.CancellationPending)
{
//Eliminar os ficheiros do Serviço Antigo
UpdateMessage("A preparar os ficheiros para actualizar o cliente");
//Program.Funcoes.DeleteAll(Global.OldService);
bwAsync.ReportProgress(30);
}
if(!bwAsync.CancellationPending)
{
//Eliminar os ficheiros do Serviço Antigo
//Program.Funcoes.DeleteAll(Global.OldClient);
UpdateMessage("A preparar os ficheiros para actualizar o serviço");
bwAsync.ReportProgress(40);
}
if(!bwAsync.CancellationPending)
{
//Copiar os Novos Ficheiros
UpdateMessage("A actualizar o Cliente");
Program.Funcoes.DirectoryCopy(Global.NovoCliente, Global.OldClient, true, false);
bwAsync.ReportProgress(50);
}
if(!bwAsync.CancellationPending)
{
UpdateMessage("A actualizar o Serviço");
Program.Funcoes.DirectoryCopy(Global.NovoServiço, Global.OldService, true, false);
bwAsync.ReportProgress(55);
}
if(!bwAsync.CancellationPending)
{
//Fazer as alterações ao Orca.config.exe
UpdateMessage("A configurar o Cliente");
WriteConfigOrca(Global.OldClient);
Program.Funcoes.UpdateCliente(Global.OldClient + @"\Orca.exe.config", Global.NovoCliente + @"\Orca.exe.config");
bwAsync.ReportProgress(70);
}
if(!bwAsync.CancellationPending)
{
UpdateMessage("A configurar o Serviço");
Program.Funcoes.UpdateService(Global.OldService + @"\OrcaService.exe.config", Global.NovoServiço + @"\OrcaService.exe.config");
//WriteConfigOrcaService(Program.OldService + "\\OrcaService.exe.config");
bwAsync.ReportProgress(85);
}
if(!bwAsync.CancellationPending)
{
//Instalar o serviço
UpdateMessage("A instalar o novo Serviço");
Program.Funcoes.InstallService(Global.OldService + "\\OrcaService.exe");
Thread.Sleep(100);
bwAsync.ReportProgress(100);
}
return;
}显然,如果您需要在取消员工时进行一些清理,请在返回之前进行清理。
https://stackoverflow.com/questions/36721458
复制相似问题