首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止在新线程上的后台工作人员,如何取消当工作人员有多个函数?

如何停止在新线程上的后台工作人员,如何取消当工作人员有多个函数?
EN

Stack Overflow用户
提问于 2016-04-19 14:32:14
回答 1查看 80关注 0票数 0

我调用了CancelAsync,但是在我的DoWork functio中,我不知道在什么地方检查取消事件的位置--在这么多需要有序执行的函数中,不管现在是什么函数,它都要停止:

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-19 14:41:09

CancelAsync()方法只会在工作线程上将bwAsync.CancellationPending标志设置为true。为了使取消操作实际进行,每当您认为进程准备被取消时,您必须验证该标志,如下所示:

代码语言:javascript
复制
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;
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36721458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档