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

  • Home
  • SEARCH
  • 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 8137933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:20:14+00:00 2026-06-06T11:20:14+00:00

I have a very basic windows forms app, working in C#, and I was

  • 0

I have a very basic windows forms app, working in C#, and I was trying out the Background Worker. I followed the base code on that page for the most part.

    void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
            progressBar.Value = e.ProgressPercentage;
    }

Everything works great, except for if the background worker is doing work when the window is closed while it is running (e.g. hitting the X). The above method is throwing a null reference exception (I presume on the progressBar?).

I did put this on the closing method:

    private void Form_FormClosing(object sender, FormClosingEventArgs e)
    {
        if(bgWorker.WorkerSupportsCancellation == true)
            bgWorker.CancelAsync();
    }

But, this did not solve the issue. I’m wondering why this is happening, I know it’s isolated to the progressBar, because if I remove that line no exceptions are thrown when I close the window while it’s busy.

  • 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-06-06T11:20:18+00:00Added an answer on June 6, 2026 at 11:20 am

    Yes, when you close the Window then the controls including the ProgressBar are destroyed. Trying to access them from the Bgw without checking will result in an error.

    You can safeguard like this:

    void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
       if ((progressBar == null) || progressBar.IsDisposed)
           return;
    
       progressBar.Value = e.ProgressPercentage;
    }
    

    And it is a good idea to Cancel the Bgw as well, but CancelAsync() by itself is not enough. The code in DoWork() needs to check the Cancellation flag.

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

Sidebar

Related Questions

I have some very basic semaphore code that works great on Linux, but cannot
I have a very basic doubt regarding the method that gets executed when app
I have a very basic data class that is subclassed from NSObject. I declare
I have a very basic script that calculates and changes the width of a
I have a very basic model - User I am trying to do some
I have a very basic question about the LocalMon print monitor found at http://msdn.microsoft.com/en-us/library/windows/hardware/ff556478%28v=vs.85%29.aspx
I'm trying to do something very basic here, something I wouldn't have expected to
I have only very basic skills writing batch files for windows. I'd like to
I have a very basic ccnet.config file, in which I am trying to use
I have (what should be) a very simple Windows Forms application. There is a

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.