.NET Programmer Thoughts

Sunday, February 06, 2005

Using a Timer In A Windows Service

I've created my first windows service in .NET! It was so easy! The hard part was getting the timer to work!

My service is named DBMSWatcher. All it does is check to make sure that a program (process) is running. If it isn't, it tries to start it and if it can't restart it, it will send an email to someone to let them know it's not running. During this time the application log is also being updated.

The first thing you have to do is create a Windows Service project. Just like you would create a windows forms project, locate the Windows Service template. It's that simple!

Most examples on the net will tell you just to add a timer control from the toolbar. Well, that isn't going to work. Using a System.Timers.Timer won't work either. At least it didn't for me. I had to use a System.Threading.Timer. Switch to the code view and you will see override methods for OnStart and OnStop.

Sorry, the code is in VB.NET again. There are lots of sources on the net to explain about to create a windows service. The key here is just to let you know the toolbar timer isn't going to work. And I couldn't get the System.Timers.Timer to work. The only way I could get a timer to work was to use the System.Threading.Timer.

Create a Private timer.

Private oTimer As System.Threading.Timer

In the OnStart method, type the following code.

Dim oCallback as New TimerCallback(AddressOf OnTimedEvent)
oTimer = New System.Threading.Timer(oCallback, Nothing, 60000, 60000)

Then create a method for the OnTimedEvent as follows.

Private Sub OnTimedEvent(ByVal state As Object)

Dim myLog As New EventLog
If Not myLog.SourceExists("DBMSWatcher") Then

myLog.CreateEventSource("DBMSWatcher", "DBMSWatcher Log")

End If
myLog.Source = "DBMSWatcher"
myLog.WriteEntry("Something Happened!", EventLogEntryType.Information)



Now look how easy it is to install the service. Go to the design windows and right click on it. Select Add Installer. It will add an installer project with two controls. ServiceProcessInstaller1 and ServiceInstaller1. Select the ServiceInstaller1 control and open the property window. Change the "ServiceName" and the "DisplayName" to what ever you want your service called. Select the ServiceProcessInstaller1 control and change the Account property to "LocalSystem".

Now build the application and create an executable.

To install the service you nned to use the InstallUtil program. You can find it in c:\windows\microsoft.net\framework\v.x.x.xxxx\. If you go to the VS .NET command prompt you will be pathed to the folder. (To get to the command prompt, select Start -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools -> Visual Stuido .NET Command Prompt)

Type in the follow command in the command window.

InstallUtil "c:\program dir\DBMSWatcher.exe"

Now you need to start the service. Open the control panel and open the Administrative Tools. Click on Services, and locate your service. Right click on it and select Start to start it.

When you need to install a new version you have to stop the service and then uninstall it. Type in the following to uninstall a service.

InstallUtil /u "c:\program dir\DBMSWatcher.exe"


First you have to start your service. In the VS .NET IDE go to Debug -> Processes. Find DBMSWatcher.exe and attach the debugger. Then you will be able to set your breakpoints as usual. If you want to debug the OnStart procedure you need to place a "Stop" in the code