|
VB.NET • Create a MonitorWrapper Class Listing 2. You can implement a wrapper class to hide the details of how monitoring is implemented within the Windows Service. This allows you to make changes later without affecting your client applications. Imports System.ServiceProcess
Public Class MonitorWrapper
Private Shared _strMachineName As String = "."
Private Shared _strMonitorServiceName As String _
= "AppMonitor"
Public Enum MonitorCode
MON_GATE_A_OPEN = 128
MON_GATE_B_OPEN = 129
MON_FIREALARM_A = 130
MON_SPRINKLER_A = 131
End Enum
Public Enum MonitorRC
RC_SUCCESS = 0
RC_NOSERVICE = -1
RC_SERVICESTOPPED = -2
RC_SERVICECOMMANDFAIL
End Enum
Public Shared Function MonitorThis(ByVal iCode _
As MonitorCode)
Dim objSvcHandle As _
ServiceProcess.ServiceController
' Open a handle to the AppMonitor service
objSvcHandle = New _
ServiceController(_strMonitorServiceName, _
_strMachineName)
' Make sure handle was made (maybe the service
' is not installed)
If (objSvcHandle Is Nothing) Then
Return MonitorRC.RC_NOSERVICE
End If
' Make sure the AppMonitor service is started
If (objSvcHandle.Status <> _
ServiceControllerStatus.Running) Then
Return MonitorRC.RC_SERVICESTOPPED
End If
Try
' Execute the command
objSvcHandle.ExecuteCommand(iCode)
Return MonitorRC.RC_SUCCESS
Catch ex As Exception
Return MonitorRC.RC_SERVICECOMMANDFAIL
End Try
End Function
End Class
|