Starting an application without showing a form

There were a lot of good comments/questions about keeping your UI responsive, if you're interested in threading/application contexts etc, it's worth a peek.

I thought I'd rehash something today I've already talked about, but it is buried in my how to build notify icon (system tray) applications article, and I think it's worthy of it's own coverage here: how to run an application without showing a form

Dissecting Application.Run

If you have created a Windows Forms application, you may have happened across this line in Main:

Application.Run(new Form1());

Magically, after calling this, the Form appears and events start firing left and right.  Here’s what is actually happening, not quite so magically:  

//Taking a microscope to Application.Run –
//create the form, and call initialize component

Form1 form1 = new Form1();

// create an application context

ApplicationContext applicationContext = new ApplicationContext();

// Set applicationContext.MainForm so that
// when form1 closes, exit the application context

applicationContext.MainForm = form1;

// Call Application.Run which will
//  -  call applicationContext.MainForm.Show()
//  -  begin processing events

From this code, we can intuit that the ApplicationContext defines when the Application should exit.  In fact the point of all the overloads to Application.Run is to help the Application know when to quit.

Running an application without showing a form

There are really two options here:

Instead of calling Application.Run with an argument, call Application.Run() to start processing messages with NO arguments, then Application.Exit() when finished.  This is not very elegant as a missed call to Application.Exit means your application will keep running even though there is no UI present.

A much cleaner solution is to inherit from the application context class and redefine when you think the application should quit.  The second solution is covered in depth with sample here.

Comments (3)

  1. jfoscoding says:

    Steps for generating this in VB 2.0

    a. Create new VB windows forms application

    b. Select Project->Add Module menu item

    Name the Module "Program.vb" (the name doesnt actually matter)

    c. Open up "Program.vb" in the code editor

    d. Add the following lines

    <STAThread()> Sub Main()

    MessageBox.Show("Now calling main!")

    Application.Run(new Form1)

    End Sub

    e. In the solution explorer, double click on My Project

    f. Uncheck the "Enable Application Framework" checkbox

    g. Change the Startup object to "Sub Main"

    h. Save and run the app. Verify you see "Now calling main!" messagebox.

    i. Now we’ve got a main, you can write your own application context and replace the code in Sub Main to use your own Application Context.

    <STAThread()> Sub Main()

    Dim applicationContext As MyApplicationContext

    applicationContext = New MyApplicationContext()


    End Sub

    Class MyApplicationContext

    Inherits System.Windows.Forms.ApplicationContext

    ‘ To be implemented…

    End Class

  2. jfo's coding says:

    Custom PaintingPainting best practices ComboBox OwnerDrawLayoutDock layout/Using the Splitter control…

  3. jfo's coding says:

    The notify icon article started out because I realized, even internally that the best practice for this…

Skip to main content