简体   繁体   中英

Cancel out of DoWork and fall into RunWorkerCompleted

I have a BackgroundWorker in a WPF application. If a condition is true, I want to immediately stop processing the _DoWork method and go straight to the _RunWorkerCompleted . I'm using .CancelAsync , but the code after this point continues to execute.

How can I cancel out of my _DoWork and fall into _RunWorkerCompleted ?

Example:

private BackgroundWorker step1 = new BackgroundWorker();

public MyWindow()
{
    InitializeComponent();
    step1.WorkerSupportsCancellation = true;
    step1.DoWork += new DoWorkEventHandler(step1_DoWork);
    step1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(step1_RunWorkerCompleted);
}

private void step1_DoWork(object sender, DoWorkEventArgs e)
{
    if (someCondition)
    {
        step1.CancelAsync();                
    }
    // code I do not want to execute
}

private void step1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // I want to jump here from the cancel point
}

CancelAsync is a method designed to be called by something other than the DoWork handler to indicate that the DoWork handler should stop executing. The DoWork handler should be checking to see if the BGW has requested cancellation and if so, stop executing (either by returning, throwing an exception, or otherwise not performing further work).

In your DoWork handler, check the cancellation state.

private void step1_DoWork(object sender, DoWorkEventArgs e)
{
    if (someCondition)
    {
        step1.CancelAsync();                
    }

    if (!step1.CancellationPending)
    {
        // code I do not want to execute
    }
    else
    {
        e.Cancel = true;
        return;
    }
}

    private void startAsyncButton_Click(object sender, EventArgs e)
    {
        if (backgroundWorker1.IsBusy != true)
        {
            // Start the asynchronous operation.
            backgroundWorker1.RunWorkerAsync();
        }
    }

    private void cancelAsyncButton_Click(object sender, EventArgs e)
    {
        if (backgroundWorker1.WorkerSupportsCancellation == true)
        {
            // Cancel the asynchronous operation.
            backgroundWorker1.CancelAsync();
        }
    }

    // This event handler is where the time-consuming work is done.
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; i <= 10; i++)
        {
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                break;
            }
            else
            {
                // Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500);
                worker.ReportProgress(i * 10);
            }
        }
    }

    // This event handler updates the progress.
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
    }

    // This event handler deals with the results of the background operation.
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled == true)
        {
            resultLabel.Text = "Canceled!";
        }
        else if (e.Error != null)
        {
            resultLabel.Text = "Error: " + e.Error.Message;
        }
        else
        {
            resultLabel.Text = "Done!";
        }
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM