Problems with SetForegroundWindow Calls

June 3rd 2006 Win32 Windows

Either as a user or as a developer you have certainly noticed that sometimes the application just flashes in the taskbar instead of actually coming to the foreground when the SetForegroundWindow function is called. What you might not know is why and when this happens.

As far as the why goes, the Application Compatibility Toolkit's Compatibility Administrator puts it very nicely in the GiveupForeground compatibility fix description: In Windows XP the foreground semantics have been changed to stop foreground focus stealing by one application if another application is active.

Further investigation reveals that this is related to the ForegroundLockTimeout value. It defines how much time must pass since the last user input to allow another process to force its window into the foreground. Before that time such a window only flashes in the task bar. The default value is 200000 milliseconds. The setting is stored in the registry:

HKEY_CURRENT_USER\Control Panel\Desktop\ForegroundLockTimeout

The value can be programmatically changed by calling the SystemParametersInfo function as follows:

SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0,
    SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);

The downside is that the call only succeeds when the calling thread has permission to change the foreground window which usually isn't the case.

To make the long story short: you should never depend on being able to bring your application window to the foreground and this will certainly only get more restrictive in the future. If the flashing in the taskbar is not enough, you should consider using tray balloon pop-ups as the alternative way of notifying the user.

Get notified when a new blog post is published (usually every Friday):

Copyright
Creative Commons License