2015-11-10 2 views
0

J'utilise un arrière-plan avec des paramètres multiples. Le travailleur travaille avec mes paramètres. Mais mon problème est, je ne peux pas arrêter l'arrière-plan.C# Travailleur en arrière-plan avec plusieurs paramètres n'annulant pas

.... 
BackgroundWorker workerGetAdvData; 
workerGetAdvData = new BackgroundWorker(); 
workerGetAdvData.DoWork += new DoWorkEventHandler(getAdvData.request_DoWork); 

workerGetAdvData.RunWorkerAsync(clsComSettingMain); 

........ 


class ClsGetAdvData 
{ 
    //open Serial Port with settings from clsComSettings class 
    byte[] adv_request = { 0xF0, 0x02, 0x0D }; //Command for requesting advanced sensor data from PFC 

public void request_DoWork(object sender, DoWorkEventArgs e) 
{ 
    ClsComSettingMain clsComSettingMain = (ClsComSettingMain)e.Argument; 
    string comPort = clsComSettingMain.comport; 
    int baudRate = clsComSettingMain.baudRate; 

    if (comPort != null && baudRate != 0) 
    { 
     SerialPort serialPort = new SerialPort(comPort, baudRate); 
     serialPort.Open(); 

     while (true) 
     { 
      if (e.Cancel) 
      { 
       e.Cancel = true; 
       break; 
      } 
      else 
      { 
       serialPort.Write(adv_request, 0, 3); // Write byte array to serial port, with no offset, all 3 bytes 
       Thread.Sleep(500); 
      } 
     } 
    } 

} 

}

J'essaie d'annuler le travailleur de fond avec le code suivant:

 private void MenuItem_Click_Serial_Stop(object sender, RoutedEventArgs e) 
    { 
     if (workerGetAdvData.IsBusy) //check if worker is running 
     { 
      workerGetAdvData.CancelAsync(); 
     } 
    } 

}

Mais le travailleur ne s'arrête pas. Ça ne marche pas à cause des paramètres que j'utilise chez le travailleur?

Amitiés Bastian

meilleures salutations

Répondre

2

A l'intérieur du code DoWork vous devriez vérifier BackgroundWorker.CancellationPending Property comme celui-ci

public void request_DoWork(object sender, DoWorkEventArgs e) 
{ 
    var worker = (BackgroundWorker)sender; 
    ClsComSettingMain clsComSettingMain = (ClsComSettingMain)e.Argument; 
    string comPort = clsComSettingMain.comport; 
    int baudRate = clsComSettingMain.baudRate; 

    if (comPort != null && baudRate != 0) 
    { 
     SerialPort serialPort = new SerialPort(comPort, baudRate); 
     serialPort.Open(); 

     while (true) 
     { 
      if (worker.CancellationPending) 
      { 
       e.Cancel = true; 
       break; 
      } 
      else 
      { 
       serialPort.Write(adv_request, 0, 3); // Write byte array to serial port, with no offset, all 3 bytes 
       Thread.Sleep(500); 
      } 
     } 
    } 

} 

Vous pourriez avoir facilement répondu à cette question vous-même en lisant simplement le CancelAsync méthode documentation.

+0

Merci! Cela a résolu mon problème. –

+0

@BastiAn Bienvenue. Si cela fonctionne pour vous, vous pouvez envisager d'upvoting et/ou d'accepter la réponse. De cette façon, vous gagnerez quelques points de réputation et aussi plus d'attention à vos questions futures. –