July 05, 2007

Windows Services

I would write more articles if things worked right and I understood everything. But sometimes things go wrong and I don't really know why and I can't be bothered to find out why. About a year ago I followed some articles online and wrote a Windows Service in VB.NET. Everything worked great. I had a service I could start and stop. The service had its own event log. Yesterday I tried to recreate the service for another project. It didn't want to create its own event log, and that's why I can't write about it.

But I don't care. Maybe if I solve the problem, I'll add a comment to this entry but here is a guide on writing a Windows Service using VB.NET. There are other guides available. Its actually quite simple so any guide will do. This is mine.

If you have MS Visual Studio .NET, you will probably have a project type option called Windows Service. Select that! The project loads and your workspace is all gray. Click inside the gray area and click on Add Installer. That will create another gray area with two things inside. You have to edit a few properties on the Service Installer and Service Process Installer such as:


  1. The account under which the service will run

  2. The service name

  3. The service description



Now you need to get installutil.exe from somewhere. I think its in the dotnet framework. Put it in your bin folder. Write a couple batch programs to install and uninstall the service. If your service compiles to Service.exe your install script will be
installutil Service.exe
Your uninstall script will be
installutil /u Service.exe

Build your project and run the install. Go to Services (Administrative Tools) and manually start your service. There... it runs. Now stop the service. And run the uninstall script. You have to do this everytime before you compile your app. Remember to stop your service and uninstall. If you don't you may have to reboot.

There are two service methods in your service. OnStart and OnStop. They are pretty self-explanatory. I won't get into using the event log. I couldn't get that to work right. Now believe it or not, most of that was fluff. The real point of this article is to show you how to write an app that sleeps and works.

We will be using a thread to sleep and work. OnStart is where you create and start the thread. A thread that sleeps and works needs to know two things: How long to sleep and what to do when it wakes up. In OnStart we define these two things.

Protected Overrides Sub OnStart(ByVal args() As String)

'Set the sleep interval
lngSleepInterval = 60000

'Set the thread to process the running method
Thread = New System.Threading.Thread(AddressOf Me.running)

' Start the new thread.
Thread.Start()
End Sub

Yes we defined lngSleepInterval as an instance variable outside the function. Bad form I know but who cares. Me.running is of course our worker function.

Public Sub running()
Try
runningx()
Catch ex As Exception
'Do some error handling
End Try
End Sub

Hmmm our worker function actually calls another worker function encased in a try...catch block. How interesting. Error handling removed because its proprietary. Here is what our actual worker does.

Public Sub runningx()
blnRunning = True

Dim blnSkipInterval = False

While (blnRunning)

'Do work here


'Sleep after running
If Not blnSkipInterval Then
System.Threading.Thread.CurrentThread.Sleep(lngSleepInterval)
Else
blnSkipInterval = False
End If
End While
End Sub

End Class

blnRunning is our stop condition. Set this to false and our thread will stop after its current run. Sometimes we work lazily and sometimes we work diligently. Our thread only stops working if there's no work to do, and the first step of any job is to check if there's any work to do. Thus, after finishing our present task, we should not sleep. Rather we should check if we have more work to do. This is what blnSkipInterval will do.

To stop the worker thread, we look a OnStop. Inside we merely set the blnRunning flag to false and wait for the app to stop.

Protected Overrides Sub OnStop()
blnRunning = False
End Sub


That is Windows Services in a nutshell. Thanks for reading.

No comments: