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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:12:22+00:00 2026-06-14T20:12:22+00:00

I have a class that raises an event after a specified time (it uses

  • 0

I have a class that raises an event after a specified time (it uses a System.Timers.Timer inside). In my test code, I created a Stopwatch which I started before the class was created and set the callback for the event to stop that Stopwatch. Then, I blocked until Not Stopwatch.IsRunning. Simple, right?

My original blocking code was

While Stopwatch.IsRunning
End While

but I found that having an empty while loop like that never allowed my callback to fire! As soon as I put debugging code into the while loop, it worked!:

Dim lastSecond As Integer = 0
While sw.IsRunning
    If (Date.Now.Second > lastSecond) Then
         lastSecond = Date.Now.Second
         Console.WriteLine("blocking...")
    End If
End While

What causes this strange behavior, and more importantly, what’s the simplest code I can put into my blocking section that will allow the event to fire?

  • 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-14T20:12:23+00:00Added an answer on June 14, 2026 at 8:12 pm
    While Stopwatch.IsRunning
    End While
    

    It is one of the Great Sins in threading, called a “hot wait loop”. Threading has many sins, and many of them have no yellow tape at all, but this one is particularly insidious. The principal problem is that you keep one processor core burning red hot, testing the IsRunning property in a tight loop.

    This begets a very nasty problem when you use the x86 jitter, it generates code in the release build that reads the IsRunning property backing field variable in a cpu register. And tests the cpu register value over and over again, without reloading the value from the field. That’s the ultimate deadlock, it can never exit the loop. You bumped it out of that mode by editing the code or by using the debugger. To avoid it, the backing field of the property must be declared volatile but that’s not something you can do in VB.NET, nor is it the proper fix.

    Instead you should use a proper synchronization object, one that lets you signal another thread that something happened. A good one is the AutoResetEvent, you’d use it like this:

    Dim IsCompleted As New AutoResetEvent(False)
    
    Private Sub WaitForTimer()
        IsCompleted.WaitOne()
        ''etc..
    End Sub
    
    Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As EventArgs) Handles timer.Elapsed
        IsCompleted.Set()
        timer.Stop()
    End Sub
    

    Beware that AutoResetEvent has yellow tape missing as well. Calling Set() more than once while the other thread hasn’t yet called WaitOne() ends up poorly.

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

Sidebar

Related Questions

I have a class that raises an event with an error message. In some
I have a poker blind timer Silverlight app that is losing time (after running
If I have a class that raises an event, with (e.g.) FrobbingEventArgs, am I
I have the following code that I'd like to test: public class DirectoryProcessor {
I have a backend class that has to raise an event with no arguments.
I have an asynchronous class with a StartProcessing() method, that raises an int ResultReady()
I have some jQuery code that finds elements with the shaded CSS class and
I have a base class that contains the following events: public event EventHandler Loading;
I have a base page, BasePage, that raises an event that displays messages to
I have some code that raises PropertyChanged events and I would like to be

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.