How to handle idle time processing: When idle is not really idle

Did you know that on fast machine idle time processing can kick in even if from you point of view application is not really idle? Take, for example, MFC. It calls OnIdle when application message queue becomes empty. Now, imagine that you are typing something. On fast machine application message queue may become empty between keystrokes since fast box can finish processing of the keystroke well before you type the next character. In fact, even if you press ahd hold a key, forcing fast repeating character input, application still may hit idle.

If idle time processing cannot be split into small chunks, it entering idle when user is, in fact, still typing, may really hurt application UI responsiveness. For example, VS HTML editor performs parse at idle time and we really don’t want this to happen when you are still typing since parser pass is an atomic action at least in the current version.

So we really must ensure that idle is really idle. The best way to achieve that, is to call GetLastInputInfo  Win32 API and then check if difference between dwTime field in the LASTINPUTINFO structure and the current tick count (see GetTickCount) is large enough (like, 500ms or so) before you execute any idle time code. GetLastInputInfo is avaialble on Windows 2000 and higher.

Here is some old, but still relevant MSJ article on the subject.

Comments (4)

  1. Tene says:

    Any idea how to apply this to .NET?

    I know there is an Application.Idle event, but when I do something on this idle period, I get high cpu use… looks like it’s exactly the same idle as MFC provides.

    I need this for two reasons: update UI (toolbars, menus, …) in my app (is there any other way? .NET 2.0, so with the toolstrip stuff). And perform a redrawing of a preview.

    Maybe it’s not the right place to ask, but well I was just thinking bout it 😉

  2. nagaraju says:



  3. jtalbut says:

    If anyone wants it, I’ve got the hardcopy of that MSJ up for sale on ebay (UK) at the moment.

    Sorry for the advertising, I just don’t want to have to recycle them and I have to get rid of them.

  4. Several people reported issues when toolbox, validation and/or server code intellisense do not work reliably