After showing and closing a dialog win32 GetForegroundWindow() still returns that window
<p>My application doesn't have a main Form. Initialization is done in Program.Main() and then Application.Run() is called.</p>

<p>When the user is in a different application they press a keyboard shortcut which triggers the following in my application:</p>

<ol>
<li>Instantiate my form.</li>
<li>Show the form.</li>
<li>Call the win32 API GetForegroundWindow() to find out which window is active.</li>
<li>Do some stuff.</li>
<li>Call form.Close()</li>
<li>Set form = null</li>
</ol>

<p>I can execute the above a few times and all is well. However, if my application calls MessageBox() at any time all subsequent calls to GetForegroundWindow() will return the form which was closed even if the current focus is in a different application. This also happens if CredUIPromptForCredentials() prompted the user for a login and password. The call ProcessInfo.GetActiveProcess() also returns my process instead of the other app that has the focus.</p>

<p>Any idea what I might be doing wrong? Is there maybe something I can call to remove the focus from my app; kind-of the reverse of SetForeGroundWindow?</p>

<p>EDIT 1</p>

<p>It's actually a non-modal form, not a dialog. I'm using Show() with no argument to show the form. I tried calling Dispose() after Close() but the behavior is the same. I confirmed that the app works correctly if I never call Show() on my form. The form has all the defaults you get when you create it in VS2010 except for changing the title. The form only contains one label.</p>
