AddThis Social Bookmark Button

Print

Debugging Windows Services
Pages: 1, 2

Now, create a new Windows application to test the DLL. In my Windows application, I need to add a reference to the DLL created above (see Figure 5).

Creating a reference to the TimeService class
Figure 5: Creating a reference to the TimeService class

Once it is done, add three buttons to the Windows form to Stop, Pause, and Continue the service.

I have the following global variables:


Dim timeservice As New TimeServiceClass.TimeService
Public Delegate Sub ServiceDelegate()

Note that I have created a delegate called ServiceDelegate() so that I can invoke the timeservice object asynchronously in my form load event:


Private Sub Form1_Load(ByVal sender As System.Object, _
                       ByVal e As System.EventArgs) _
                       Handles MyBase.Load
    Dim async As New ServiceDelegate(AddressOf StartService)
    async.BeginInvoke(Nothing, Nothing)
End Sub
   

The StartService() method simply starts the service by invoking the StartService() method of the timeservice object:


  Public Sub StartService()
      timeservice.startService()
  End Sub
   

Likewise, I do the same for the Stop, Pause, and Continue buttons:


Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button1.Click
    ' Stop the service
    timeservice.stopService()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button2.Click 
    ' Pause the service
    timeservice.pauseService()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button3.Click
    ' Continue the service
    timeservice.continueService()
End Sub
   

You can now test your time service by running the Windows application, checking to see if you can stop, pause, and continue, your service.

Once you are sure that the service logic is working correctly, you can use the service class in your actual Windows service. Here is the code for the Windows service that uses the TimeService class (remember to add a reference to the class):


Imports System.ServiceProcess
Public Class Service1
  Inherits System.ServiceProcess.ServiceBase

  Dim timeservice As New TimeServiceClass.TimeService
  Public Delegate Sub ServiceDelegate()
  Dim log As New System.Diagnostics.EventLog

  '---------------------------------------------------------------
  Protected Overrides Sub OnStart(ByVal args() As String)
    Dim async As New ServiceDelegate(AddressOf StartService)
    async.BeginInvoke(Nothing, Nothing)

    Me.AutoLog = False
    If Not EventLog.SourceExists("Time Service Log") Then
      EventLog.CreateEventSource("Time Service Log", "MyLogFile")
    End If
    log.Source = "Time Service Log"
    log.WriteEntry("Service Started")
  End Sub
  '---------------------------------------------------------------
  Protected Sub StartService()
    timeservice.startService()
  End Sub
  '---------------------------------------------------------------
  Protected Overrides Sub OnStop()
    timeservice.stopService()
    log.WriteEntry("Service Stopped")
  End Sub
  '---------------------------------------------------------------
  Protected Overrides Sub OnPause()
    timeservice.pauseService()
    log.WriteEntry("Service Paused")
  End Sub
  '---------------------------------------------------------------
  Protected Overrides Sub OnContinue()
    timeservice.continueService()
    log.WriteEntry("Service Continued")
  End Sub
End Class
   

You can now build the Windows service, install it, start it, and it should work correctly!

Summary

Encapsulating Windows services logic within a class allows you to debug the service easily, without the hassles of attaching a debugger to a running service. It is the recommended way to build Windows services. Using a combination of the techniques--attaching a debugger, encapsulating the service logic, and using the event log--will make life as a Windows service developer much easier and more productive.

Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.


Return to ONDotnet.com