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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:42:56+00:00 2026-05-23T00:42:56+00:00

I noticed that sometimes my code becomes out of sync if an event fires

  • 0

I noticed that sometimes my code becomes out of sync if an event fires too quickly. I was wondering if there was a better approach. Under a normal scenario the DeviceOpenedEvent fires after I tell the thread to WaitOne in the TestDevice method, but I have seen in some cases where the event gets fired before the thread has a chance to wait.

    protected AutoResetEvent TestAutoResetEvent = new AutoResetEvent(false);
    public EventEnum WaitForEvent = EventEnum.None;

    bool TestDevice()
    {
        OpenDevice();

        WaitForEvent = EventEnum.DeviceOpened;
        TestAutoResetEvent.WaitOne();
        WaitForEvent = EventEnum.NoWait;

        //Continue with other tests
    }

    void DeviceOpenedEvent()
    {
        if (WaitForEvent == EventEnum.DeviceOpened)         
            TestAutoResetEvent.Set();                           
    }

Under normal circumstances it looks like this:

  1. Open Device
  2. WaitOne()
  3. DeviceOpenedEvent occurs
  4. Set()

This is what I’m seeing my logs sometimes:

  1. Open Device
  2. DeviceOpenedEvent occurs
  3. WaitOne() Essentially stuck here forever
  • 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-23T00:42:57+00:00Added an answer on May 23, 2026 at 12:42 am

    Since OpenDevice is asynchronous (as you mentioned in a comment), it runs in a different thread than its caller. Sometimes it will finish before the next line in source executes:

        OpenDevice(); // Async: may finish before the next line executes!
        WaitForEvent = EventEnum.DeviceOpened;
    

    When that happens DeviceOpenedEvent doesn’t do what you want it to, because WaitForEvent is still EventEnum.None:

    if (WaitForEvent == EventEnum.DeviceOpened)         
        TestAutoResetEvent.Set(); 
    

    The solution is to change your code so that you signal completion inside a method that’s guaranteed to run in the correct order. Here’s a simple implementation that removes the enumeration and uses a single wait handle for each event you need to wait on:

    protected AutoResetEvent deviceOpenedEvent = new AutoResetEvent(false);
    protected AutoResetEvent deviceLockedEvent = new AutoResetEvent(false);
    
    bool TestDevice() {
        OpenDevice();
        // Do some unrelated parallel stuff here ... then
        deviceOpenedEvent.WaitOne();
        LockDevice();
        deviceLockedEvent.WaitOne();
    }
    
    void DeviceOpenedEvent() {
        deviceOpenedEvent.Set();                           
    }
    

    It’s even easier if you control OpenDevice: just call deviceOpened.Set() when it’s done. You could even change OpenDevice to accept the auto reset event and construct it right inside TestDevice, which would reduce your exposure to multithreading bugs.

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

Sidebar

Related Questions

In Java, I noticed that sometimes, System.err statements get printed first before System.out statements,
I noticed that Datatables sometimes generates inline CSS (style='....'). Is there are way to
I am using Source->Implement Method sometimes, but I noticed that the generated code does
I have noticed that sometimes people have to use multiple versions of jQuery in
I am reading up about Encoding and Decoding and I noticed that sometimes people
I have noticed that my application's Document folder is sometimes different. The workflow was
I noticed, that sometimes (especially where mod_rewrite is not available) this path scheme is
I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The
Looking at the code for some big websites (as I sometimes do) I noticed
I noticed that sometimes(randomly) my Mootools ajax request gets send twice. The second request

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.