Click here to Skip to main content
Click here to Skip to main content

Formless Notify Icon Application

, 21 Apr 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Shows notify icon, starting an app without a form, unhandled exception handling

Introduction

There are enough samples on notification icons, but most use third party components that contain the icon while .NET 2.0 offers a notification icon. Furthermore all sample applications start with a form that they show the user and keep in memory at all times. When the user closes the form, they hide it. I needed an application that only creates a form when the user needs to do some settings and closes the form without keeping it in memory. (Since the app was running on XPembedded where we need all the memory we can get free). This sample runs on XP as on XPembedded. It shows how to start an application without a form, and the use of the notification icon with a context menu to create and show a form to let the user do some settings. When the user is done, the form is removed from memory. It also shows how to make a class into a singleton and how to build an unhandled exception handler.

Using the Code

The code is only meant as an example. It does nothing useful. It does show how to start an application without a form (not hiding it!). This is rather simple (if you know how), simply change one line of code in the generated code in program.cs.

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    /******************** NO Form *******************************************
    /* Here you create the class that will serve as the main
    * instead of loading the form and then hiding it. 
    * The class should call Application.Exit() when exit time comes.
    * The form will only be created and kept in memory when it's actually used.
    * Note that Application.Run does not start any form
    * Checker is a Singleton
    ************************************************************************/
    Checker checker = Checker.GetCheckerObject();
    //Application.Run(new Form1());
    Application.Run();
    checker = null; //must be done to stop referencing Checker so GC can collect it.
}

Using the notification icon is straight forward, it does not need a form:

//notifyicon
notify = new NotifyIcon();
notify.Icon = iconstop;
notify.Text = "VriServerManager";
notify.ContextMenu = contextmenu;
//show form when double clicked
notify.DoubleClick += new EventHandler(notify_DoubleClick); 
notify.Visible = true;

A singleton is an object that can only have one instance in existence. One would choose a singleton when the object lives during the entire life span of the application and when the work done by the object only makes sense when done by one instance. You can make a class a singleton by making the constructor private, instantiate it during the first reference and add a method that enables other objects to obtain a reference to it.

Like so: Private variable to instance:

private static readonly Checker checker = new Checker();

Private constructor:

private Checker() //singleton so private constructor!
{
}

Method to get reference:

public static Checker GetCheckerObject()
{
    return checker;
}

Points of Interest

This application contains code to catch exceptions that are not caught by the application itself. Usually this will result in an unhandled exception message box with information that is almost impossible to interpret. When using the unhandled exception event, you can get the information you desire like the line number where the problem originates from. This is all you need to do in the main...

AppDomain myCurrentDomain = AppDomain.CurrentDomain;
myCurrentDomain.UnhandledException += 
    new UnhandledExceptionEventHandler(myCurrentDomain_UnhandledException);

The handler...

static void myCurrentDomain_UnhandledException
    (object sender, UnhandledExceptionEventArgs e)
{
    //get error info here, see program.cs
}

History

  • 21-04-2007: First draw

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

mdv113
Web Developer Siemens
Netherlands Netherlands
Windows/Web developer for Windows Or embedded.
Started with ASM85 / PLM85
Then C and C++ (STL)(Windows / Linux)
ATL / MFC
Now mostly working with .net C# / VB
ASP.NET and ADO.NET and some SQL (sql 2000 & 2005)

Comments and Discussions

 
Generalsimilar PinmemberAlexHaan26-Apr-07 23:20 
AnswerRe: similar Pinmembermdv11327-Apr-07 0:53 
GeneralRe: similar PinmemberGigio2k3-Oct-08 10:49 
GeneralRe: similar Pinmembermdv1135-Oct-08 21:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 21 Apr 2007
Article Copyright 2007 by mdv113
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid