VB.NET • Control a Windows Service With the ServiceController Object

Listing 3. Utilize the MonitorWrapper class within the sample security control application to send service commands to the AppMonitor Windows Service, and control the status of the AppMonitor service through the ServiceController object.

Private Sub btnOpenGateA_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles btnOpenGateA.Click
   SendMonitorCommand _
      (MonitorWrapper.MonitorWrapper. _
      MonitorCode.MON_GATE_A_OPEN)
End Sub

Private Sub btnOpenGateB_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles btnOpenGateB.Click
   SendMonitorCommand _
      (MonitorWrapper.MonitorWrapper. _
      MonitorCode.MON_GATE_B_OPEN)
End Sub

Private Sub btnFireAlarmZoneA_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles btnFireAlarmZoneA.Click
   SendMonitorCommand _
      (MonitorWrapper.MonitorWrapper. _
      MonitorCode.MON_FIREALARM_A)
End Sub

Private Sub btnSprinklerZoneA_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles btnSprinklerZoneA.Click
   SendMonitorCommand _
      (MonitorWrapper.MonitorWrapper. _
      MonitorCode.MON_SPRINKLER_A)
End Sub

Private Sub btnStartService_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles btnStartService.Click
   ' Find service
   Dim objServiceController As New _
      ServiceController("AppMonitor", ".")

   ' See if the service is valid on this machine
   If (objServiceController Is Nothing) Then
      MessageBox.Show _
         ("Could Not Find AppMonitor Service On This Machine")
      Return
   End If

   ' See if it's stopped
   If (objServiceController.Status = _
      ServiceControllerStatus.Stopped) Then
      ' Start the service
      Try
         objServiceController.Start()
         ' Now wait for the service to start (10 
         ' seconds)
         objServiceController.WaitForStatus( _
            ServiceControllerStatus.Running, _
            New TimeSpan(0, 0, 10))
         MessageBox.Show( _
            "AppMonitor Service Started: Status = " + _
            objServiceController.Status.ToString)
      Catch ex As Exception
         MessageBox.Show( _
            "Unable To Start AppMonitor Service")
      End Try
   Else
      ' Service wasn't stopped
      MessageBox.Show _
         ("AppMonitor Service Wasn't Stopped - " & _
         "Unable To Start It")
   End If
End Sub

Private Sub btnStopService_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles btnStopService.Click
   ' Find service
   Dim objServiceController As New _
      ServiceController("AppMonitor", ".")

   ' See if the service is valid on this machine
   If (objServiceController Is Nothing) Then
      MessageBox.Show _
         ("Could Not Find AppMonitor Service On This Machine")
      Return
   End If

   ' See if it's running
   If (objServiceController.Status = _
      ServiceControllerStatus.Running) Then
      ' Start the service
      Try
         objServiceController.Stop()
         ' Now wait for the service to stop (10 
         ' seconds)
         objServiceController.WaitForStatus( _
            ServiceControllerStatus.Stopped, _
            New TimeSpan(0, 0, 10))
         MessageBox.Show( _
            "AppMonitor Service Stopped: Status = " + _
            objServiceController.Status.ToString)
      Catch ex As Exception
         MessageBox.Show( _
            "Unable To Stop AppMonitor Service")
      End Try
   Else
      ' Service wasn't started
      MessageBox.Show _
         ("AppMonitor Service Wasn't Started - " & _
         "Unable To Stop It")
   End If
End Sub
Private Sub SendMonitorCommand(ByVal cmd As _
   MonitorWrapper.MonitorWrapper.MonitorCode)
   Dim rc As MonitorWrapper.MonitorWrapper.MonitorRC
   rc = MonitorWrapper.MonitorWrapper.MonitorThis( _
      cmd)
   Select Case rc
      Case MonitorWrapper.MonitorWrapper. _
         MonitorRC.RC_NOSERVICE
         MessageBox.Show( _
            "AppMonitor Service Not Found")
      Case MonitorWrapper.MonitorWrapper. _
         MonitorRC.RC_SERVICECOMMANDFAIL
         MessageBox.Show( _
            "Error Sending Command to Service")
      Case MonitorWrapper.MonitorWrapper. _
         MonitorRC.RC_SERVICESTOPPED
         MessageBox.Show( _
            "Unable to Send Command - Service Stopped")
      Case MonitorWrapper.MonitorWrapper. _
         MonitorRC.RC_SUCCESS
   End Select
End Sub