Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6578459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:46:25+00:00 2026-05-25T15:46:25+00:00

I have a sample WinForms application that uses the BackgroundWorker component. It works fine

  • 0

I have a sample WinForms application that uses the BackgroundWorker component. It works fine but when I hit the Cancel button to cancel the background thread it doesn’t cancel the thread. When I hit the Cancel button to call .CancelAsync() method, then in RunWorkerCompleted event handler e.Cancelled Property always remains false. I think when I hit Cancel button it should be set to true.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 1; i <= 100; i++)
    {
       // Wait 100 milliseconds.
       Thread.Sleep(100);
       // Report progress.
       if (backgroundWorker1.CancellationPending == true)
       {
           //label1.Text = "Cancelled by user.";
           break;
        }

        backgroundWorker1.ReportProgress(i);
     }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Change the value of the ProgressBar to the BackgroundWorker progress.
    progressBar1.Value = e.ProgressPercentage;
    // Set the text.
    label1.Text = e.ProgressPercentage.ToString();
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled == true)
    {
        label1.Text = "Canceled!";
    }
    else if (e.Error != null)
    {
        label1.Text = "Error: " + e.Error.Message;
    }
    else
    {
         label1.Text = "Done!";
    }
}

private void button2_Click(object sender, EventArgs e)
{
    if (backgroundWorker1.WorkerSupportsCancellation == true)
    {
        // Cancel the asynchronous operation.
        backgroundWorker1.CancelAsync();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:46:26+00:00Added an answer on May 25, 2026 at 3:46 pm

    The Cancelled property is still false because you break out of the loop and then allow the backgroundworker’s DoWork function to end in the normal way. You never tell your backgroundworker component that the pending cancel was actually accepted.

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 1; i <= 100; i++)
        {
            // Wait 100 milliseconds.
            Thread.Sleep(100);
    
            if (backgroundWorker1.CancellationPending)
            {
                e.Cancel = true;
                break;
            }
    
            // Report progress.
            backgroundWorker1.ReportProgress(i);
        }
    }
    

    The distinction is important, because sometimes you may want rollback work you’ve already done when you detect the CancellationPending request, and so it may be a while before you actually finish with the cancel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms Form that used to be a standalone application, but is
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a simple winforms application that I am connecting to my database with
I have a winforms application that includes an Administration form (call it 'adminForm') and
Background: I have created sample windows application for learning to implement localization. My each
I have a winforms application that doesn't really have a main form - it's
I have developed a simple C# Winforms application that loads MS-Word 2007 documents via
I have an Application in C#/Winforms that is basically used to run test for
I have made a simple test application for the issue, two winforms each containing
I've started working on a little ruby project that will have sample implementations of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.