Click here to Skip to main content
15,868,051 members
Articles / Programming Languages / Visual Basic
Article

Formless Notify Icon Application

Rate me:
Please Sign up or sign in to vote.
3.91/5 (15 votes)
21 Apr 2007CPOL2 min read 77.9K   4.2K   59   11
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.

C#
[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:

C#
//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:

C#
private static readonly Checker checker = new Checker();

Private constructor:

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

Method to get reference:

C#
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...

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

The handler...

C#
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)


Written By
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

 
Generalvery cool Pin
mboiciuc7-Nov-08 4:30
mboiciuc7-Nov-08 4:30 
GeneralRe: very cool Pin
mdv1137-Nov-08 11:36
mdv1137-Nov-08 11:36 
GeneralA little improvement Pin
adgonz26-Sep-08 4:47
adgonz26-Sep-08 4:47 
GeneralRe: A little improvement Pin
mdv11326-Sep-08 5:07
mdv11326-Sep-08 5:07 
QuestionTooltip and BalloonTips not show. Pin
qjchen28-Mar-08 11:35
qjchen28-Mar-08 11:35 
AnswerRe: Tooltip and BalloonTips not show. Pin
mdv1131-Sep-08 22:55
mdv1131-Sep-08 22:55 
Generalsimilar Pin
AlexHaan26-Apr-07 22:20
AlexHaan26-Apr-07 22:20 
AnswerRe: similar Pin
mdv11326-Apr-07 23:53
mdv11326-Apr-07 23:53 
GeneralRe: similar Pin
Gigio2k3-Oct-08 9:49
Gigio2k3-Oct-08 9:49 
GeneralRe: similar Pin
mdv1135-Oct-08 20:00
mdv1135-Oct-08 20:00 
GeneralNice Article Pin
Bishoy Demian21-Apr-07 22:08
Bishoy Demian21-Apr-07 22:08 

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

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