Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I do periodic checks in an OnIdle handler or a timer?

Tags:

delphi

I want to log the user out of my application after a period of inactivity.

Would it be better to put my check for inactivity in the applciation's OnIdle handler or in a timer handler?

Granularity is not too important. Logout can be plus/minus a few seconds, maybe even a minute.

I get the feeling that the app's OnIdle is going to be firing too often. Any advice? The code iteself is only a few lines.

like image 263
Mawg says reinstate Monica Avatar asked Oct 04 '12 03:10

Mawg says reinstate Monica


1 Answers

You are using GetLastInputInfo to measure idle time. That seems very sensible. So the question boils down to

Should I do periodic checks in an OnIdle handler or a timer?

OnIdle fires every time the message queue is emptied. If no messages are placed in the queue, OnIdle will never fire. So, OnIdle is not periodic. If you want something to check inactivity regularly, then OnIdle may not work. A timer is certain to work.

You can think of it like this: the OnIdle event fires when the idle counter starts, but you need an event to fire when the idle counter expires.

I get the feeling that the app's OnIdle is going to be firing too often.

In fact your problem is the opposite of this!


Note that you may experiment with this and find that OnIdle appears to work adequately, and fire regularly. For example, if your app uses any timers then OnIdle will fire following each timer event. Because the timer event comes in on the queue and once it is processed, OnIdle is fired. But if your app has no timers then you can expect OnIdle not to fire at all if you stop interacting with your program.

like image 148
David Heffernan Avatar answered Oct 04 '22 20:10

David Heffernan