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 6584743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:34:31+00:00 2026-05-25T16:34:31+00:00

I have a simple BackgroundWorker defined that uses closures for its DoWork and RunWorkerCompleted

  • 0

I have a simple BackgroundWorker defined that uses closures for its DoWork and RunWorkerCompleted event handlers, which set and check a boolean value, respectively. In simplified form, the code (C# in .NET 3.5) looks like this:

    public void SomeMethod()
    {
         BackgroundWorker worker = new BackgroundWorker();

         bool result = false;
         worker.DoWork += new DoWorkEventHandler(
             (sender, e) =>
             {
                 result = LengthyOperation(); 
             });

         worker.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(
             (sender, e) => 
             {
                 AppendRunLog("Result: " + (result ? "PASS" : "FAIL"));
             });

         worker.RunWorkerAsync();
     }

SomeMethod is called from a WPF button handler on the main UI thread.

Usually this works just fine, but every so often I get a report that the operation has failed when it should have passed (according to logs generated inside LengthyOperation), i.e. the boolean result gets logged as false in the Completed handler when the operation should have returned true in the DoWork handler.

I scoured and tested the heck out of the code inside LengthyOperation to make sure I didn’t have some subtle bug inside it, and I am fairly confident it is clean. I have not been able to reproduce it in my development environment.

Do I have a race condition in how I am setting and reading the result value? I would expect that the assignment operation would have completed in DoWork before the Completed event is fired.

  • 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-25T16:34:31+00:00Added an answer on May 25, 2026 at 4:34 pm

    Looks like it should be fine to me, assuming that the BackgroundWorker infrastructure makes sure there are appropriate memory barriers.

    Are you sure you don’t just have two concurrent operations, and you’re seeing two sets of logs at the same time?

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

Sidebar

Related Questions

I have simple SSIS package in which On Error event handler I have created
I have a sample WinForms application that uses the BackgroundWorker component. It works fine
I have simple regex \.*\ for me its says select everything between and ,
I have simple win service, that executes few tasks periodically. How should I pass
I have simple SSIS package which reads data from flat file and insert into
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a small app that a client uses to manually update some data.
I have a form that spawns a BackgroundWorker, that should update form's own textbox
I have a simple program. Its job is to move files from A to
I'm learning winforms and I have set myself a simple goal of making 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.