I mean exit to system tray, not minimize to system tray, ie when you click on the "red cross" in the top right corner on a Windows form, instead of application closing, it runs in the system tray instead.
You can hook to Form Closing event and cancel the closing event and hide the form
sample code
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
Handle the FormClosing event, block the Close (e.Cancel = true
) and instead minimize to the system tray as you would if you were, well, minimizing to the system tray.
You will, of course, have to have a condition under which closing the form doesn't minimize to the system tray. For example, tracking the state and if the form is already minimized then allowing the form to actually close.
It is not possible to 'exit' to the system tray, a process with a message queue must be running to allow an icon to appear in the tray and function correctly (e.g. not disappear when you hover over it)
The way adobe and others handle this is to have a separate application that just does the system tray icon, when you click it and your application isn't running it starts it, if the application is already running, it brings it to the foreground.