mobContextMenu.MenuItems(3).Enabled = False
Case ServiceProcess.ServiceControllerStatus.Stopped
mobNotifyIcon.Icon = New Icon(\"Stopped.ico\")
mobContextMenu.MenuItems(0).Enabled = False
mobContextMenu.MenuItems(1).Enabled = False
mobContextMenu.MenuItems(2).Enabled = False
mobContextMenu.MenuItems(3).Enabled = True
Case _
ServiceProcess.ServiceControllerStatus.ContinuePending, _
ServiceProcess.ServiceControllerStatus.PausePending, _
ServiceProcess.ServiceControllerStatus.StartPending, _
ServiceProcess.ServiceControllerStatus.StopPending
mobNotifyIcon.Icon = New Icon(\"Paused.ico\")
mobContextMenu.MenuItems(0).Enabled = False
mobContextMenu.MenuItems(1).Enabled = False
mobContextMenu.MenuItems(2).Enabled = False
mobContextMenu.MenuItems(3).Enabled = False
End Select
\'//检查“暂停”和“继续”使用可用
If mobServiceController.CanPauseAndContinue = False Then
mobContextMenu.MenuItems(1).Enabled = False
mobContextMenu.MenuItems(2).Enabled = False
End If
Catch obEx As Exception
Throw obEx
End Try
End Sub
下面建立菜单项的事件处理程序:
\'//停止服务的过程
Private Sub StopService(ByVal sender As Object, ByVal e As EventArgs)
Try
If mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Running Then
If mobServiceController.CanStop = True Then
mobServiceController.Stop()
End If
End If
Catch obEx As Exception
Throw obEx
End Try
End Sub
\'//暂停服务的过程
Private Sub PauseService(ByVal sender As Object, ByVal e As EventArgs)
Try
If Not mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then
If mobServiceController.CanPauseAndContinue = True Then
mobServiceController.Pause()
End If
End If
Catch obEx As Exception
Throw obEx
End Try
End Sub
\'//继续服务程序的过程
Private Sub ContinueService(ByVal sender As Object, ByVal e As EventArgs)
Try
If mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then
If mobServiceController.CanPauseAndContinue = True Then
mobServiceController.Continue()
End If
End If
Catch obEx As Exception
Throw obEx
End Try
End Sub
\'//开始服务程序的过程
Private Sub StartService(ByVal sender As Object, ByVal e As EventArgs)
Try
If mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
mobServiceController.Start()
End If
Catch obEx As Exception
Throw obEx
End Try
End Sub
\'//“关于”菜单项的过程
Private Sub AboutBox(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim obStringBuilder As New StringBuilder()
With obStringBuilder
.Append(\"Service Controller 使用例子\")
.Append(vbCrLf)
.Append(\"CLR 版本:\")
.Append(Environment.Version.ToString)
MsgBox(.ToString, MsgBoxStyle.Information)
End With
obStringBuilder = Nothing
Catch obEx As Exception
Throw obEx
End Try
End Sub
\'//退出服务程序的过程
Private Sub ExitController(ByVal sender As Object, ByVal e As EventArgs)