Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

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?
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

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 https://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx#windows_messages
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

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."