2016-04-19 12 views
0

我所說的CancelAsync但在我的DoWork函數的我不能找出其中以檢查需要在orded被執行許多功能,取消事件IM中間它停止不管什麼它現在的功能:如何停止一個新線程的後臺工作人員,當工人有多個功能時如何取消?

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

     } 

我需要在哪裏檢查cancelPending?

+4

我想你只需要檢查它到處。如果你想要一個響應式的「取消」,那麼你可能想要在每一個ReportProgress()之間進行檢查。 – Quantic

+0

Ur right:D做出回答,所以我可以將它標記爲正確:DI需要票價,您是第一個回答 –

+0

@ cokeman19這不是重複的,因爲我想知道如何檢查這麼多功能。您所引用的另一個線程並不能說明這些情況,並且由於我無法在任何地方發表評論,所以我提出了一個新問題xD –

回答

1

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

顯然,如果你需要一些清理,同時取消職工辦,在你回來之前這樣做。

+0

是的你是對的。謝謝您:D –

+0

如果可能的話,您可能想要避免所有這些退貨和一個危險的代碼...我會重新格式化它,以便您不需要所有這些回報。 – Adwaenyth

+0

爲什麼bwAsync.CancelAsyn()實際上並不取消線程? –