如何停止新线程上的后台工作程序,当工作程序具有多个函数时如何取消

本文关键字:工作程序 函数 取消 何取消 新线程 线程 后台 何停止 | 更新日期: 2024-10-20 17:10:39

我调用了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;
            }
        }

我需要在哪里检查取消待定?

如何停止新线程上的后台工作程序,当工作程序具有多个函数时如何取消

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

显然,如果您在取消工作人员时需要进行一些清理,请在返回之前进行清理。