See the question and my original answer on StackOverflow

Windows restricts which processes can get and can set the foreground window to prevent focus stealing, for security and end-user experience reasons mostly. The quite complex rules are detailed here: AllowSetForegroundWindow function Remarks

But what's interesting is a process that can set the foreground window can enable another process to set the foreground window by calling AllowSetForegroundWindow, which is what you want.

In your case, you don't know which (future) process will need the focus, so you can call AllowSetForegroundWindow(ASFW_ANY) so all processes will be enabled to set the foreground window.