0

I call the CancelAsync but in my DoWork functio I cant figure out where to check for that cancellation event im middle of so many functions that need to be executed in orded for it to stop regardless of what function its now:

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?

Mr.Toxy
  • 357
  • 4
  • 19
  • 4
    I think you just need to check it everywhere. If you want a responsive "cancel" then you probably want to put a check in between every single `ReportProgress()`. – Quantic Apr 19 '16 at 14:34
  • Ur right :D make an answer so I can mark it as correct :D I need to be fare, you were the first one to answer – Mr.Toxy Apr 19 '16 at 14:46
  • @cokeman19 It's not a duplicate since I wanted to know how to check for cancellation in between so many functions. The other thread you refered doesnt clarify those situations, and Since I cant comment anywhere I made a new question xD – Mr.Toxy Apr 19 '16 at 14:50
  • 1
    Perhaps, but the comment you proposed accepting as an answer was specifically addressed in the accepted answer of the cited duplicate. "Your DoWork delegate that is being run in the background _must periodically check this property_ and handle the cancellation itself." In any case, if the community disagrees with my flag, it will be disregarded. – cokeman19 Apr 19 '16 at 15:02
  • @cokeman19 Yes you're rigth. Thank you for flagging, non the less will raise more attention to these types of questions if anyone has the same problem. – Mr.Toxy Apr 19 '16 at 15:03

1 Answers1

1

The CancelAsync() method does nothing else but set the bwAsync.CancellationPending flag to true on the worker's thread. In order for the cancellation to actually do anything, you have to verify the flag whenever you deem the process ready to be canceled, something like this:

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

Obviously if you need some cleanup to do while cancelling the worker, do that before you return.

Adwaenyth
  • 2,020
  • 12
  • 24
  • Yes you are right. Thank you :D – Mr.Toxy Apr 19 '16 at 14:47
  • If possible you might want to avoid all those returns and a hazardly looking code... I'll reformat it, so that you don't need all those returns. – Adwaenyth Apr 19 '16 at 14:52
  • Why does the bwAsync.CancelAsyn(), doesnt actually cancel the thread? – Mr.Toxy Apr 19 '16 at 14:54
  • Exactly because you might need to do some cleanup. Like dispose of temporary files you created, undo changes etc.. If the thread would just be stopped, you can't access the resources in the thread any longer. – Adwaenyth Apr 19 '16 at 14:56
  • Ok awesome, ur right that does look way better and really more well strucutured. Thank you good Sir – Mr.Toxy Apr 19 '16 at 14:57
  • There isn't any pratcical way to undo changes upon cancellation right? – Mr.Toxy Apr 19 '16 at 14:59
  • use an `else` clause and then manually undo the changes up to that point... `BackgroundWorker` basically is meant for simple but time consuming things, if you want to go deep, you'll likely have to implement your own multi threading logic. – Adwaenyth Apr 19 '16 at 15:01
  • Ok thank you for your answer :D – Mr.Toxy Apr 19 '16 at 15:05