I\'m using SetForegroundWindow API in .NET using PInvoke.
When I use the API while debugging in Visual Studio its works perfectly. But it doesn\'t work always when
Register a Hot Key with RegisterHotKey. Choose carefully the Hot Key, as you must not interfere with existing (or future) application.
When you need to steal the focus (but remember it's BAD), simulate the Hot Key with SendInput.
Then, you will receive a WM_HOTKEY message, and during the processing of that message, you will be allowed to use SetForegroundWindow (I mean, it will success).
You will have to store/remenber somewhere the HWND of the window to activate, between the call to SendInput and your processing of WM_HOTKEY, which is posted.
References: Pressing a registered hotkey gives you the foreground activation love