如何停止新线程上的后台工作者,当工作者具有多个功能时如何取消?

How to stop a background worker thats on a new thread, how to cancel when worker has multiple functions?

我调用了 CancelAsync,但在我的 DoWork 函数中,我无法弄清楚在这么多需要执行的函数中间检查取消事件的位置,以使其停止,而不管它现在是什么函数:

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;
            }

        }

Where do I need to check for that cancellationPending?

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;
}

显然,如果您在取消 worker 时需要做一些清理工作,请在 return.

之前完成