Search code examples
c#multithreadingbackgroundworker

Cannot stop the BackgroundWorker


I know that this question has posted several times, but this situation is different. So supposed that I'm executing a method that need to iterate through several items (database rows), this require a lot of time.
Now in my BackgroundWorker I need in some case to stop the synchronization, in particular when the user press a button. What I did in the _DoWork event is this:

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    while (!worker.CancellationPending)
    {
        LongOperation();
    }
}

now the problem's that when I call worker.CancelAsync() the LongOperation() continue the execution but shouldn't! 'cause the while have the condition of CancellationPending. I saw in the net that this solution is thread-safe, so maybe am I doing something wrong?


Solution

  • All you need is the following structure

    private void runButton_Click(object sender, EventArgs e)
    {
        worker=new BackgroundWorker();
    
        worker.WorkerSupportsCancellation=true;
        worker.RunWorkerCompleted+=Bk_RunWorkerCompleted;
        worker.DoWork+=Bk_DoWork;
        worker.RunWorkerAsync();
    }
    
    private void cancelButton_Click(object sender, EventArgs e)
    {
        worker.CancelAsync();
    }
    
    void ReallyReallyLongOperation(BackgroundWorker worker)
    {
        ...within a loop
        if(worker.CancellationPending) 
        {
            return;
        }
    }
    
    private void Bk_DoWork(object sender, DoWorkEventArgs e)
    {
        ReallyReallyLongOperation(worker);
        if(worker.CancellationPending)
        {
            e.Cancel = true;
        }
    }
    
    private void Bk_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if(!e.Cancelled)
        {
            ...
        }
    }