Process.CloseMainWindow() not working
<p>I start the Windows On-Screen-Keyboard like that:</p>

<pre><code>s_onScreenKeyboard = new Process();
s_onScreenKeyboard.StartInfo = new ProcessStartInfo("osk.exe");
s_onScreenKeyboard.EnableRaisingEvents = true;
s_onScreenKeyboard.Exited += new EventHandler(s_onScreenKeyboard_Exited);
s_onScreenKeyboard.Start();
</code></pre>

<p>This works fine, but when I try to stop it using the following code, it does not work, i.e. the OSK keeps running and the method returns <code>false</code>:</p>

<pre><code>s_onScreenKeyboard.CloseMainWindow();
if (!s_onScreenKeyboard.HasExited)
{
    if (!s_onScreenKeyboard.WaitForExit(1000))
    {
        s_onScreenKeyboard.Close();
        //s_onScreenKeyboard.Kill();
    }
}
</code></pre>

<p>When uncommenting <code>s_onScreenKeyboard.Kill();</code> it is closed, but the problem is that osk.exe obviously uses another process called "msswchx.exe" which is not closed if I simply kill the OSK process. This way, I would end up with hundreds of these processes which is not what I want.</p>

<p>Another strange thing is that the <code>CloseMainWindow()</code> call worked at some time, but then it suddenly did not work anymore, and I do not remember what has changed.</p>

<p>Any ideas?</p>

<p><strong>EDIT:</strong> I have found a solution myself. Please <a href="http://stackoverflow.com/questions/2948510/process-closemainwindow-not-working/2948847#2948847">see my answer</a> for details.</p>

<p><strong>Background:</strong><br>
I am implementing an On-Screen-Keyboard for my application because it should work with a touchscreen. It is important that the keyboard layout matches the layout which is configured in Windows since the application will be shipped to many different countries. Therefore, instead of implementing a custom keyboard control with approx. 537 keyboard layouts (exaggerating a little here...), I wanted to utilize the Windows built-in On-Screen-Keyboard which adapts to the selected keyboard layout automatically, saving a lot of work for me.</p>
