Search code examples
c#.netmultithreadingbackgroundworkercancellation

C# Backgroundworker w/ multiple parameters not cancelling


i'm using a backgroundworker with mutliple parameters. The worker is working with my parameters. But my problem is, I can't stop the backgroundworker.

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

}

}

I'm trying to cancel the background worker with following code:

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

}

But the worker does not stop. It it not working because of the paramters I use in the worker?

Kind regards Bastian

best regards


Solution

  • Inside the your DoWork code you should be checking BackgroundWorker.CancellationPending Property like this

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

    You could have easily answered that question yourself by simply reading the CancelAsync method documentation.