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);
Checker checker = Checker.GetCheckerObject();
Application.Run();
checker = null;
}
Using the notification icon is straight forward, it does not need a form:
notify = new NotifyIcon();
notify.Icon = iconstop;
notify.Text = "VriServerManager";
notify.ContextMenu = contextmenu;
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()
{
}
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)
{
}
History