How should application be using ForegroundLockTimeout registry value?

CJ7 picture CJ7 · Nov 5, 2013 · Viewed 12.8k times · Source

If an application invokes and activates another application there can be the issue of the invoked application not being brought to the foreground.

One work-around is to set the HKCU\Control Panel\Desktop\ForegroundLockTimeout registry value to 0 instead of the default 200000 milliseconds.

How should an application be controlling this registry value? Would it be feasible to temporarily change it to achieve the work-around and then change it back?

Answer

Hans Passant picture Hans Passant · Nov 5, 2013

Hacking the registry on the fly is never an acceptable workaround. If an application owns the foreground window then it won't have any trouble activating the window of another application. It can be done explicitly with the AllowSetForegroundWindow() winapi function. The linked MSDN page also lists the exact rules. Also helps to find the SPI_SETFOREGROUNDLOCKTIMEOUT argument for the SystemParameterInfo function.

There's a dirty undocumented hack to break the rules. I won't go into great detail about it, other than mentioning that it is actually used in the .NET framework. Since most of your questions are .NET questions I'll assume it applies, you can use Microsoft.VisualBasic.Interaction.AppActivate() method. Not that easy to use btw.

Do keep in mind that users absolutely hate this kind of hack. Make sure your uninstaller is sound.