IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

April (2018)

(1 item)

August (2014)

(1 item)

July (2014)

(5 items)

April (2014)

(1 item)

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

You Don't Need a 'Main' Form

Tuesday 30 November, 2004, 02:39 PM

A Windows Forms question that seems to crop up a lot on newsgroups and mailing lists is:

How do I hide my main form?

While it's a frequently asked question, on closer inspection it usually turns out to be the wrong question.

The context for the question is usually that someone wants the application to run as an icon in the notification area. (Also sometimes called, incorrectly, the system tray.) They don't actually want a window to appear when they run the application, or be able to continue running when all windows are closed.

So they try to make it invisible, or mess with its ShowInTaskBar property, and go through all sorts of hoops to make sure that the main form doesn't appear anywhere.

These people would have had a much easier time of it if only they had known this simple but useful fact:

You don't have to have a main form.

You get to choose whether or not to have a main form when you start the event loop in Windows Forms. The VS.NET wizards tend to give you something like this:

Application.Run(new Form1());

If you pass in a form to Application.Run like this, that form will be designated as the 'main form', and the application will run until that form closes. So how do you run without the main form? It's really rather simple:

Application.Run();

This means your application will run until you call Application.Exit(), so you'll need to make sure you do that at some point. The usual way to do that is that you'll create a NotifyIcon, and give it a menu item with an 'Exit' item, and quit the application from there. Obviously you need to set up that NotifyIcon before you call Application.Run.

The only moderately tricky aspect of this is handling shutdowns and logouts. If you have a main form, your application will end up exiting when the user logs out or shuts down. But if your application doesn't have a main form, you need to detect when this happens and exit. It's not hard to do this, you just need to remember to do it - if you don't you'll slow users down when they log out - Windows will wait a few seconds for your application to exit, and eventually ask if the user wants to nuke it, claiming that it seems to have crashed. You probably don't want your users to think your application crashes whenever Windows shuts down... So you just need to handle the SessionEnded event on the Microsoft.Win32.SystemEvents class.

Copyright © 2002-2024, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk