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?
deleydSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
AndyAinscowFreelance 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.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deleydSoftware EngineerAuthor 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."
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.