Published: 03 Jan 2007
By: Steven Swafford

Creating an application to sit in your system tray is much easier than you may think and to prove this point I will provide a step by step example of controlling an Oracle 10gR2 database service.

Introduction

I do a lot of development that is targeted toward an Oracle database instance thus I have an installation of Oracle 10gR2 on my laptop. If you fall into this category as well I am sure that you will agree that oracle can consume a great deal of resources which can place a significant drag on your system. I realize that I may be in the minority here but it irritates me that my system seems sluggish because of the oracle service.

Now I also realize that I can set the service to a manual startup or even possibly created a batch file to stop and start this service but I wanted a solution that would allow me to quickly determine if this service is running or not. Thus the idea of using the system tray hit me.

Getting Started

Open Visual Studio and create a new Windows Application project and provide a meaningful name, in this case I have used Oracle Service Controller.


Figure 1

The next step is to add a new class named MainClass to this project and add the appropriate references as indicated below.

In this example we will be working with a NotifyIcon ContextMenu, Timer, and ServiceController object so we need to go ahead and declare these objects.

The next step in to esablish a Main method which will fire when the application is executed.

You will probably notice that in the above code I am calling a method named StartApplication, this method is the core of the application and perform numerous actions. I am going to break down each of these actions into section in hope that the example is easier to follow.

StartApplication Method Explained

At the core of this application we need to accomplish the following:

  1. Pull in the application settings from the app.config.
  2. Create the ServiceController object.
  3. Create the NotifyIcon object.
  4. Create the ContextMenu object.

CreateMenu Method Explained

If you are not familiar with a ContextMenu take a moment to read the MSDN Documentation. Basically a context menu is nothing more than a shortcut menu and if you have ever right clicked on any of the icons that may be sitting in your system tray you have used a context menu.

Next I will create my ContextMenu which will contain the following five items:

  1. Stop
  2. Start
  3. Spacer
  4. About
  5. Exit

SetupTimer Explained

The timer we will use serves only one purpose which is to change the state of the NotifyIcon based upon the ServiceController status.

As the timer has elapsed we need to get the status of the ServiceController. This is accomplished by the following method.

GetServiceStatus Method Explained

In this method I determine the status of the service, set the appropriate icon, and build the context menu.

Stopping and Starting the Service

To stop the service take a look at the code below. The three steps I am taking are:

  1. Determine if the service is running.
  2. Determine if the service can be stopped.
  3. Stop the service.

To start the service take a look at the code below. Starting the service is very similar to stopping the service. The three steps I am taking are:

  1. Determine if the service is stopped.
  2. Start the service.

Exiting the Application

When it is time to exit the application it is very important to clean up the resources that we were using. As you can see I am performing actions such as stopping the timer and disposing of the objects I had created. Once again this is an important step as you should always clean up behind yourself. Do you not recall your mother always yelling at you to clean up?

Summary

Once you have compiled your project and run the executable you should notice a new icon in your system tray.

We have accomplished all of this with very little effort as A Windows Application and I hope that I have provided the groundwork for you to create your own application or expand upon this example. For example, in my source code which you can download at Radical Development I have incorporated an about form.

Continue reading and see our next or previous articles

About Steven Swafford

Sorry, no bio is available

View complete profile here.

You might also be interested in the following related blog posts


ASP.NET MVC2 Preview 2: Areas and Routes read more
Open SQL Port for specific IP by ASP.NET Website read more
SQL 2008 CLR Triggers, use a .NET class library in SQL using WPF read more
WSE, DIME; WCF, MTOM; OH My! read more
CodeDigest.Com Article,Codes,FAQs - April,2009 read more
Custom Controller Factory in ASP.NET MVC read more
Provisioning a UCMA application using ApplicationProvisioner.exe read more
Creating Extension Methods in VB read more
How to use Ninject with ASP.NET MVC read more
Introduction to HealthVault Development #2: Hello World read more
Top
 
 
 

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.