C# Close processes minimized to tray in a graceful way?
<p>I have an application that has can display a window using a Form. The form is only shown if the application is run using a -debug flag, otherwise it is only shown in tray.</p>

<pre><code>var form = new Form();
if(DebugMode)
    form.Show();
</code></pre>

<p>The application listens to CloseMainWindow() when run in debug mode, as the form is shown.
How can I make the application also listen to CloseMainWindow() without showing it? I don't want the user to be able to interact with the form if not in debug mode.</p>

<p>I've tried several approaches, like displaying the window but setting the size to 0. This shows a small form, i.e. not hidden.</p>

<pre><code>if (!DebugMode)
{
    form.Show();
    form.Size = new Size(0, 0);
}
</code></pre>

<p>Also showing it, and then hiding it does not work:</p>

<pre><code>if (!DebugMode)
{
    form.Show();
    form.Hide();
}
</code></pre>

<p>Showing it, but started minimized and not shown in taskbar does not work either:</p>

<pre><code>if (!DebugMode)
{
    form.Show();
    form.WindowState = FormWindowState.Minimized;
    form.ShowInTaskbar = false;
}
</code></pre>

<p>Am I missing something really obvious here, or is it not possible to close processes minimized to tray in a graceful way?</p>
