Hi I have been asked to maintain a Delphi 5 based program for someone, and the program is using a timer object to tick every 50 milli-seconds and upon each time-up it runs the block of single threaded code. I am just wondering, what would happen if the time taken to execute this block of code is longer than the timer tick interval, would this be bad? For example could it cause problems like access violation? How does Delphi handle this kind of situation by default? Thanks a lot.
The critical part of this question is :
... what would happen if the time taken to execute this block of code is longer than the timer tick interval, would this be bad?
It's not great, but it's not a show stopper and it certainly cannot cause access violations. Delphi's TTimer is implemented using the WinAPI SetTimer function.
You might naively think that if your timer's handler took longer than the interval to process that the timer would continue to pile up messages in the message queue and your program would effectively lock up with a flood of timer messages that had no hope of ever all being processed. Thankfully, this isn't exactly how timers work. The documentation can shed some light.
WM_TIMER message
The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue.
Now, there isn't really a concept of "high" and "low" priority messages in a windows application, and while this statement is a little ambiguous we can take the context to mean that WM_TIMER is a message that is not posted to the application's message queue but rather generated in response to a GetMessage or PeekMessage call when a timer has been set with SetTimer, when that timer's interval has elapsed, and when there are no other messages already in the queue.
So, while the timer interval may elapse during your handler's processing, any other messages that come in while this is happening will still enter the queue normally and will be processed once your handler completes. Only once the queue has been emptied again will another WM_TIMER message be generated.
The timer events, therefore, will execute either at the rate of the tick interval or as fast as your application can process them, whichever ends up being longest. If you do have timer messages coming in too quickly, however, and your timer handler's processing time is long then your application's responsiveness can suffer. It won't become unresponsive, but all other message processing will be restricted to being processed at the interval of your timer's event handler execution time. This can make your application feel sluggish.
Example
To demonstrate, create a new forms application and add a TTimer component with an interval set to 10. Then attach this handler :
procedure TForm1.Timer1Timer(Sender: TObject);
begin
sleep(200);
end;
While the program is running, try moving the window around. What we have done is to essentially quantize the application's message processing to a 200ms interval (the duration of the timer's event handler execution).
Ticks of the timer do not interrupt your code.
Timer ticks are delivered in the form of window messages. Window messages can only arrive when you check the message queue for new messages. That happens automatically when your timer event handler returns and your program resumes its event loop, but you can trigger it explicitly by calling Application.ProcessMessages. Don't call that, though; it seldom solves problems in the long run.
If you don't check the message queue in your timer-tick handler, then your handler will never start running a second time while it's still handling a previous tick.
Even if you do check the queue, all that happens is that the tick handler will be called recursively. After all, it's all running in a single thread. Recursive timer handling probably isn't what you want to happen, though, so I'll again counsel against checking for messages in a message handler.
Furthermore, timer messages can never "pile up" if your timer handler takes a long time to run. Timer messages are "fake" in that they don't actually get added to the message queue at regular intervals. Instead, the OS will synthesize a timer message at the time your program checks the queue for more messages. If there are no higher-priority messages in the queue, and the timer interval has elapsed, then the OS will return a wm_Timer message. If you don't check for more messages, then there will be no timer message in the queue. In particular, there will not be multiple timer messages in the queue.
Further reading:
Stack Overflow: How does the message queue work in Win32?
Dr. Dobbs: Inside the Windows Messaging System
Related
What does it mean that input sources such as port deliver events to a run loop async while timers deliver events synchronously.
Does the timer block the thread?
The Threading Programming Guide: Run Loops says:
A run loop receives events from two different types of sources. Input sources deliver asynchronous events, usually messages from another thread or from a different application. Timer sources deliver synchronous events, occurring at a scheduled time or repeating interval. Both types of source use an application-specific handler routine to process the event when it arrives.
But a timer only blocks the thread while the timer’s closure or selector method is running. But as soon as you return from that, the thread is no longer blocked. So make sure to get in and out as quickly as possible.
For example, if you have scheduled a timer to fire in 10 seconds, and the code in the timer’s handling closure/selector takes 100 msec to run, then the thread is not blocked until the timer fires, and then only for 100 msec. Same with repeating timers.
Bottom line, as long as you’re not doing anything too computationally expensive in your timer handler, there’s nothing to worry about. And if you do need to do anything that might block for any material amount of time, then either have your timer handler asynchronously dispatch that relevant code to some background queue, or just schedule a GCD timer to run on a background queue directly, bypassing Timer altogether.
But for most Timer use-cases, this just isn’t an issue.
In my application I use a winevent hook to get focus changes system-wide. Because there are no timing problems, I use an out-of-context hook, even if I know that it is slow. If there are multiple events fired quickly on after another, the system queues them and gives them to the hook callback function in the right order.
Now I would like to process only the newest focus change. So if there are already other messages in the queue, I want the callback function to stop and restart with the parameters of the newest message. Is there a way to do that?
When you receive a focus change, create an asynchronous notification to yourself, and cancel any previous notification(s) that may still be pending.
You can use PostMessage() and PeekMessage(PM_REMOVE) for that. Post a custom message to yourself, removing any previous custom message(s) that are still in the queue.
Or, you can use TTimer/SetTimer() to (re)start a timer on each focus change, and then process the last change when the timer elapses.
Either way, only the last notification will be processed once the messages slow down.
I have a program that occasionally needs to scan some directories recursively (an improvement for this part of the program is in the pipeline, but won't be ready for a while). To avoid the user having to wait for this scanning, I would like to do the scanning while the user isn't using my program when possible.
I intend to implement it by running a timer that checks for idle time. I've found the following for checking system idle time:
http://www.delphitips.net/2007/11/11/how-to-detect-system-idle-time/
This would be functional, but I would prefer to activate the function even when the user is working with other programs on the same computer. This way, if he switches to another program, I could catch up on the scanning I need to do.
I realize that I could do the scanning in a background thread, and either that or some sort of windows hooks will be implemented at some point, but not just yet.
EDIT:
The goal here is a relatively easy change to the program to do any scanning that might be queued while the user isn't actively using MY application. The scanning isn't especially intensive, but it isn't done in a thread, and therefore freezes my app while in progress. Basically, I'm looking for a quick win while I'm working on a more long term solution.
Use the Application.OnIdle event. That event is triggered when your program has no more window messages to handle. The keyboard and mouse both generate messages, so if there are no more messages, then the user is not using your program, even if it has the focus.
procedure TJasonForm.ApplicationEventsIdle(Sender: TObject; var Done: Boolean);
var
NoMoreFiles: Boolean;
begin
// TODO: Scan for the next file
Done := NoMoreFiles;
end;
As long as Done is False and there are no messages in the queue, your program will continue to call that event handler, allowing you to find more files. When the user generates some input, your program will handle the messages before calling OnIdle again.
When you've finished scanning the file system, set Done to True so that the program stops re-calling the OnIdle handler. If you neglect to do that, then your program will use all available CPU time repeatedly calling an event handler that does nothing.
For this to work, you'll need to use a non-recursive search routine. A recursive search will search the whole file system before returning, but if you do that, then the OnIdle handler will hang your program, which is the oposite of what you want. You can use a queue of directory names, and each time the event is fires, pop one item off the queue, search it, and add its contents to the end of the queue.
...but I would prefer to activate the
function even when the user is working
with other programs on the same
computer. This way, if he switches to
another program, I could catch up on
the scanning I need to do.
Stop the scanning in Application.OnActivate and resume in Application.OnDeactivate.
What you need is a separate thread that will be resumed when the system is idle or paused when there is activity. A simple way to do this is to place a Timer Component and check if the system is idle and thread is suspended or not use ResumeThread ,SuspendThread
Look here
http://pastebin.com/8X9Sg42H i crafted something for your situation enjoy
A separate thread creates a TidTCPClient and a TTimer. The TTimer is set to 3s and if the TCPClient is not connected, it calls TCPClient.Connect.
If there is no server to connect to, this results in an attempt to connect every 3 seconds.
The main thread (UI) does nothing, but if I grab the window with the mouse and move it slowly across the screen, it get's stuck every 3 seconds for about 2 seconds, then it jumps to the mouse-cursor position and follows the mouse again, until the next attempt to connect occurs.
In other words, the main thread seems to be blocked when the TCPClient tries to connect.
Why does this happen, even though the TCPClient is in it's separate thread?
Your TTimer works by receiving WM_TIMER messages; Those messages are dispatched using the VCL's main message pump, in the VCL threads. After the 3 seconds expire your TTimer.OnTimer event runs in the main thread, so the call to Connect runs in the main VCL thread.
You get blocked because you're not threading!
I have a TTimer in my application that fires every 2 seconds and calls my event handler, HandleTimerEvent(). The HandleTimerEvent() function modifies shared resources and can take 10's of seconds to execute before returning. Furthermore, I call Sleep() in the event handler to relinquish the processor at times.
I'm not sure how C++ builder's TTimer object works when it comes to calling events, so the scenario I just explained has got me thinking, particularly, whether HandleTimerEvent() gets called before a prior call has returned.
The question comes down to a couple of things.
Does the TTimer object queue the events?
Can the TTimer object call my event handler before a prior call has returned?
This reply assumes that TTimer is still implemented to use WM_Timer messages. If the implementation has changed (since 2005), please disregard.
No, the TTimer object does not queue events. It is driven by the Windows WM_Timer message, and Windows does not let WM_TIMER messages stack up in the message queue. If the next timer interval occurs and Windows sees that a WM_Timer message is already in the app's message queue, it does not add another WM_Timer messsage to the queue. (Same for WM_Paint, btw)
Yes, it is possible for a TTimer.OnTimer event to be fired even while a prior event handler is still executing. If you do anything in your event handler that allows the app to process messages, then your timer event can be reentered. The obvious one is if your event handler calls Application.ProcessMessages, but it can be much more subtle than that - if anything you call in your event handler internally calls Application.ProcessMessages, or calls PeekMessage/GetMessage + DispatchMessage, or opens a modal dialog, or calls a COM interface that is bound to an out-of-process COM object, then messages in your app message queue will be processed and that could include your next WM_Timer message.
A simple solution is to disable the timer object when you enter your timer event handler, and reenable it when you exit your timer event handler. This will prevent timer messages from firing while your event handler is still working, regardless of the message handling characteristics of your code.
I use TTimer extensively. It does not queue events. If you want it to hand off to an event handler, then create a TThread that handles your events so the Timer can continue with it's work. The timer does not operate asychronously but rather synchronously.