# Why is SPI\_SETFOREGROUNDLOCKTIMEOUT set to zero on glfwInit (Win32)?

**URL:** <https://discourse.glfw.org/t/why-is-spi-setforegroundlocktimeout-set-to-zero-on-glfwinit-win32/1874>\
**Category:** support\
**Created:** [June 17, 2021, 11:47pm UTC](https://discourse.glfw.org/t/why-is-spi-setforegroundlocktimeout-set-to-zero-on-glfwinit-win32/1874 "2021-06-17T23:47:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonimy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/bonimy/32/534_2.png) [@bonimy](https://discourse.glfw.org/u/bonimy)\
**Post date:** [June 17, 2021, 11:47pm UTC](https://discourse.glfw.org/t/why-is-spi-setforegroundlocktimeout-set-to-zero-on-glfwinit-win32/1874/1 "2021-06-17T23:47:10Z")

</div>

```c
win32_init.c:
int _glfwPlatformInit(void)
{
    // To make SetForegroundWindow work as we want, we need to fiddle
    // with the FOREGROUNDLOCKTIMEOUT system setting (we do this as early
    // as possible in the hope of still being the foreground process)
    SystemParametersInfoW(SPI_GETFOREGROUNDLOCKTIMEOUT, 0,
                          &_glfw.win32.foregroundLockTimeout, 0);
    SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, UIntToPtr(0),
                          SPIF_SENDCHANGE);

```

It says to make `setForegroundWindow` work as you want, but what way is that?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [June 21, 2021, 9:18am UTC](https://discourse.glfw.org/t/why-is-spi-setforegroundlocktimeout-set-to-zero-on-glfwinit-win32/1874/2 "2021-06-21T09:18:04Z")

</div>

Hi @bonimy,

welcome to the GLFW forum.

I think this was done in order to permit [glfwFocusWindow](https://www.glfw.org/docs/latest/group__window.html#ga873780357abd3f3a081d71a40aae45a1) to work. I did a git blame to check why it was committed, but this code goes back to GLFW 2.7 Lite.

---

<div class="post-metadata">

**Author:** ![mbitsnbites](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/mbitsnbites/32/539_2.png) [@mbitsnbites](https://discourse.glfw.org/u/mbitsnbites)\
**Post date:** [June 23, 2021, 7:54am UTC](https://discourse.glfw.org/t/why-is-spi-setforegroundlocktimeout-set-to-zero-on-glfwinit-win32/1874/3 "2021-06-23T07:54:29Z")

</div>

Hi! That would be my code, dating back to the very early 2.x (possibly 1.x) versions.

The purpose is to enable us to call `SetForegroundWindow()`. Windows has a user input timeout thingy, meaning that if you are typing things on a keyboard while `SetForegroundWindow()` is called, the request to change window focus is ignored by the system. In general this is a good thing (e.g. imagine typing in a password in one window and then suddenly you’re switched to a new window and you accidentally continue typing in your password there instead).

This code is specifically there to circumvent that timeout mechanism by assuming that early on in the application lifecycle the application has focus etc which is a requirement for the `SystemParametersInfo()` call to have effect.

Check the documentation for `SPI_GETFOREGROUNDLOCKTIMEOUT` here: [SystemParametersInfoW function (winuser.h) - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow)

It’s vague, but notice: _“The calling thread must be able to change the foreground window, otherwise the call fails.”_

IIRC the code was tested against Windows 98/ME/2000 (or thereabout). I’m not sure that it still works the same way in Windows 10.

OTOH - today (a few years wiser) I would strongly advice against using `SetForegroundWindow()` at all, so I’m not sure that this should be part of GLFW.
