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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:47:11+00:00 2026-06-16T16:47:11+00:00

This code works, for most of time, so I’m thinking of some race condition.

  • 0

This code works, for most of time, so I’m thinking of some race condition. Result class is immutable, but I don’t think the issue is with that class.

public Result GetResult()
{
    using (var waitHandle = new ManualResetEvent(false))
    {
        Result result = null;

        var completedHandler = new WorkCompletedEventHandler((o, e) =>
        {
            result = e.Result;

            // somehow waitHandle is closed, thus exception occurs here
            waitHandle.Set();
        });
        try
        {
            this.worker.Completed += completedHandler;

            // starts working on separate thread
            // when done, this.worker invokes its Completed event
            this.worker.RunWork();

            waitHandle.WaitOne();

            return new WorkResult(result);
        }
        finally
        {
            this.worker.Completed -= completedHandler;
        }
    }
}

Edit: Apologies, I’ve missed a call to this.worker.RunWork() right before calling GetResult() method. This apparently resulted (sometimes) in doing same job twice, though I’m not sure why waitHandle got closed before waitHandle.Set(), despite having Completed event firing twice. This hasn’t compromised the IO work at all (results were correct; after I’ve changed the code to manually close the waitHandle).

Therefore, Iridium’s answer should be closest answer (if not the right one), even though the question wasn’t complete.

  • 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-16T16:47:12+00:00Added an answer on June 16, 2026 at 4:47 pm

    There doesn’t seem anything particularly problematic in the code you’ve given, which would suggest that there is perhaps something in the code you’ve not shown that’s causing the problem. I’m assuming that the worker you’re using is part of your codebase (rather than part of the .NET BCL like BackgroundWorker?) It may be worth posting the code for that, in case there is an issue there that’s causing the problem.

    If for example, the same worker is used repeatedly from multiple threads (or has a bug in which Completed can be raised more than once for the same piece of work), then if the worker uses the “usual” means for invoking an event handler, i.e.:

    var handler = Completed;
    if (handler != null)
    {
        handler(...);
    }
    

    You could have an instance where var handler = Completed; is executed before the finally clause (and so before the completedHandler has been detached from the Completed event), but handler(...) is called after the using(...) block is exited (and so after the ManualResetEvent has been disposed). Your event handler will then be executed after waitHandle is disposed, and the exception you are seeing will be thrown.

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

Sidebar

Related Questions

Right now I'm trying the following code. This works most of the time (on
I will keep this quick. The attached code for the most part works i
This code works well for displaying separate xml items in my main.xml. But is
This code works fine, but I'll want to handle exception if any thing goes
This code works (when ControlType=dropDown then the background yellow ): <Window x:Class=TestCollapsed.Views.MainView xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
This code works: class Test { public: Test(string name) : _name(name) {}; bool operator()()
The following snippet of code works most of the time, except in certain windows.
I have some code like this, well here is the part that is most
This code works well: UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame]; [pickerView addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
This code works fine in Chrome. However, in Firefox, when it hits the GMxhr

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.