How does System.Windows.Forms.Timer ignore WM_TIMER messages when not idle?

"The Timer tick event will invoke the EventHandler when the UI Thread is idle and processing window messages."

I'm wondering how this works internally. How does the UI thread only process window messages when it's idle? What happens to window messages when the UI thread is not idle?

Where does the UI thread fetch window messages from when it's idle? How does it fetch them? How does it ignore them when it's busy? Why don't the window messages stack up waiting to be processed? Does the UI thread decide to subscribe to window messages when it's idle, and unsubscribe from window messages when it's busy?
Who is Participating?
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
In the background all windows messages are NOT equal.  Some will only be added to a queue when there is nothing waiting behind them to be processed.  
Messages are not fetched in windows operating system, they are placed in a queue and the window reads the queue when it is not blocked by running code.  When no messages are in the queue then a window will perform idle time processing.

(This is a 'problem' with a timer.  It does not run at a fixed interval.  It only runs when it can LATER than the specified tick setting.  eg. If you wanted a tick every second.  The next timer event might actually run after 4 seconds - but never after only .99 seconds.)
David Johnson, CD, MVPOwnerCommented:
from the windows message queue

If the form is blocked for more than a few seconds you get the unresponsive application message and the window is ghosted

if the thread is busy then the message queue is not polled this is why we use the async and await when starting long running tasks so the UI is still responsive

Better to read from the source
deleydAuthor Commented:
I found the following:

"Timer events are placed only in the application event queue if that queue is empty and the application is idle. Windows does not place timer event messages in the application event queue if the application is already busy. If your application has been busy and has missed several timer event messages, Windows places only a single timer message in the event queue. Windows does not send your application all the timer event messages that occurred while your application was busy. It doesn't matter how many timer messages your application may have missed; Windows still places only a single timer message in your queue.

"When you start or stop a timer, you specify a timer ID, which can be any integer value. Your application uses this timer ID to determine which timer event has triggered, as well as to start and stop timers."
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.