Advertisements
Creating an Oracle 10gR2 System Tray Application Controller
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 that is targeted toward an Oracle
instance thus I have an installation of Oracle 10gR2 on my 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 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.
01.
using
System;
02.
using
System.Diagnostics;
03.
using
System.Text;
04.
using
System.Timers;
05.
using
System.Windows.Forms;
06.
using
System.ServiceProcess;
07.
using
System.Drawing;
08.
using
System.Reflection;
09.
using
System.Configuration;
10.
11.
namespace
OracleServiceControler
12.
{
13.
public
class
MainClass {}
14.
}
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.
1.
private
NotifyIcon _notifyIcon =
null
;
2.
private
ContextMenu _contextMenu =
null
;
3.
private
System.Timers.Timer _timer =
null
;
4.
private
ServiceController _oracleDatabaseServiceController =
null
;
The next step in to esablish a Main
method which will fire when the application is executed.
1.
public
static
void
Main(
string
[] args)
2.
{
3.
MainClass _main =
new
MainClass();
4.
_main.StartApplication();
5.
}
You will probably notice that in the above code I am calling a method named StartApplication
, this method is the core of the 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:
- Pull in the application settings from the
app.config
. - Create the
ServiceController
object. - Create the
NotifyIcon
object. - Create the
ContextMenu
object.
01.
public
void
StartApplication()
02.
{
03.
try
04.
{
05.
string
_serviceName =
06.
ConfigurationManager.AppSettings[
"Oracle_Service_Name"
];
07.
string
_tooltip = ConfigurationManager.AppSettings[
"Notify_Icon_Tooltip"
];
08.
_oracleDatabaseServiceController =
new
ServiceController(_serviceName);
09.
10.
_notifyIcon =
new
NotifyIcon();
11.
_notifyIcon.Visible =
false
;
12.
_notifyIcon.Text = _tooltip;
13.
14.
_contextMenu =
new
ContextMenu();
15.
16.
CreateMenu();
17.
18.
_notifyIcon.ContextMenu = _contextMenu;
19.
20.
SetUpTimer();
21.
22.
_timer.Elapsed +=
new
ElapsedEventHandler(TimerElapsed);
23.
24.
_notifyIcon.Visible =
true
;
25.
26.
GetServiceStatus();
27.
28.
Application.Run();
29.
}
30.
catch
(Exception ex)
31.
{
32.
MessageBox.Show(ex.Message.ToString(),
"Oracle Service Controller"
,
33.
MessageBoxButtons.OK, MessageBoxIcon.Error);
34.
}
35.
}
CreateMenu Method Explained
If you are not familiar with a ContextMenu
take a moment to read the MSDN Documention. Basically a context menu is nothing more than a shortcut menu and if you have ever right clicked on any of the 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:
- Stop
- Start
- Spacer
- About
- Exit
01.
public
void
CreateMenu()
02.
{
03.
try
04.
{
05.
_contextMenu.MenuItems.Add(
new
MenuItem(
"Stop"
,
06.
new
EventHandler(StopService)));
07.
_contextMenu.MenuItems.Add(
new
MenuItem(
"Start"
,
08.
new
EventHandler(StartService)));
09.
10.
_contextMenu.MenuItems.Add(
"-"
);
11.
12.
_contextMenu.MenuItems.Add(
new
MenuItem(
"About"
,
13.
new
EventHandler(AboutDisplay)));
14.
_contextMenu.MenuItems.Add(
new
MenuItem(
"Exit"
,
15.
new
EventHandler(ExitController)));
16.
}
17.
catch
(Exception ex)
18.
{
19.
MessageBox.Show(ex.Message.ToString(),
"Oracle Service Controller"
,
20.
MessageBoxButtons.OK, MessageBoxIcon.Error);
21.
}
22.
}
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.
01.
private
void
SetUpTimer()
02.
{
03.
try
04.
{
05.
_timer =
new
System.Timers.Timer();
06.
_timer.AutoReset =
true
;
07.
_timer.Interval = 5000;
08.
_timer.Start();
09.
}
10.
catch
(Exception ex)
11.
{
12.
MessageBox.Show(ex.Message.ToString(),
"Oracle Service Controller"
,
13.
MessageBoxButtons.OK, MessageBoxIcon.Error);
14.
}
15.
}
As the timer has elapsed we need to get the status of the ServiceController
. This is accomplished by the following method.
01.
public
void
TimerElapsed(
object
sender, System.Timers.ElapsedEventArgs e)
02.
{
03.
try
04.
{
05.
GetServiceStatus();
06.
}
07.
catch
(Exception ex)
08.
{
09.
throw
;
10.
}
11.
}
GetServiceStatus Method Explained
In this method I determine the status of the service, set the appropriate icon, and build the context menu.
01.
private
void
GetServiceStatus()
02.
{
03.
try
04.
{
05.
_oracleDatabaseServiceController.Refresh();
06.
07.
if
(_oracleDatabaseServiceController.Status ==
08.
System.ServiceProcess.ServiceControllerStatus.Running)
09.
{
10.
Icon _startedIcon =
11.
GetIcon(
"OracleServiceControler.raddevoraserviceon.ico"
);
12.
_notifyIcon.Icon = _startedIcon;
13.
14.
_contextMenu.MenuItems[0].Enabled =
true
;
15.
_contextMenu.MenuItems[1].Enabled =
false
;
16.
_contextMenu.MenuItems[2].Enabled =
true
;
17.
_contextMenu.MenuItems[3].Enabled =
true
;
18.
_contextMenu.MenuItems[4].Enabled =
true
;
19.
}
20.
else
if
(_oracleDatabaseServiceController.Status ==
21.
System.ServiceProcess.ServiceControllerStatus.Stopped)
22.
{
23.
Icon _stoppedIcon =
24.
GetIcon(
"OracleServiceControler.raddevoraserviceoff.ico"
);
25.
_notifyIcon.Icon = _stoppedIcon;
26.
27.
_contextMenu.MenuItems[0].Enabled =
false
;
28.
_contextMenu.MenuItems[1].Enabled =
true
;
29.
_contextMenu.MenuItems[2].Enabled =
false
;
30.
_contextMenu.MenuItems[3].Enabled =
true
;
31.
_contextMenu.MenuItems[4].Enabled =
true
;
32.
}
33.
}
34.
catch
(Exception ex)
35.
{
36.
MessageBox.Show(ex.Message.ToString(),
"Oracle Service Controller"
,
37.
MessageBoxButtons.OK, MessageBoxIcon.Error);
38.
}
39.
}
Stopping and Starting the Service
To stop the service take a look at the code below. The three steps I am taking are:
- Determine if the service is running.
- Determine if the service can be stopped.
- Stop the service.
01.
private
void
StopService(
object
sender, EventArgs e)
02.
{
03.
try
04.
{
05.
if
(_oracleDatabaseServiceController.Status ==
06.
System.ServiceProcess.ServiceControllerStatus.Running)
07.
{
08.
if
(_oracleDatabaseServiceController.CanStop ==
true
)
09.
{
10.
_oracleDatabaseServiceController.Stop();
11.
}
12.
}
13.
}
14.
catch
(Exception ex)
15.
{
16.
MessageBox.Show(ex.Message.ToString(),
"Oracle Service Controller"
,
17.
MessageBoxButtons.OK, MessageBoxIcon.Error);
18.
}
19.
}
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:
- Determine if the service is stopped.
- Start the service.
01.
private
void
StartService(
object
sender, EventArgs e)
02.
{
03.
try
04.
{
05.
if
(_oracleDatabaseServiceController.Status ==
06.
System.ServiceProcess.ServiceControllerStatus.Stopped)
07.
{
08.
_oracleDatabaseServiceController.Start();
09.
}
10.
}
11.
catch
(Exception ex)
12.
{
13.
MessageBox.Show(ex.Message.ToString(),
"Oracle Service Controller"
,
14.
MessageBoxButtons.OK, MessageBoxIcon.Error);
15.
}
16.
}
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?
01.
private
void
ExitController(
object
sender, EventArgs e)
02.
{
03.
try
04.
{
05.
_timer.Stop();
06.
_timer.Dispose();
07.
_notifyIcon.Visible =
false
;
08.
_notifyIcon.Dispose();
09.
_oracleDatabaseServiceController.Dispose();
10.
Application.Exit();
11.
}
12.
catch
(Exception ex)
13.
{
14.
MessageBox.Show(ex.Message.ToString(),
"Oracle Service Controller"
,
15.
MessageBoxButtons.OK, MessageBoxIcon.Error);
16.
}
17.
}
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 at Radical Development I have incorporated an about form.