自己编写程序来控制windows服务程序的启动、暂停、继续及退出。
Imports
System.Text
Imports System.Diagnostics
Imports System.ServiceProcess
Module ModMain Module ModMain
Private WithEvents theNotifyIcon As NotifyIcon
Private WithEvents theTimer As Timers.Timer
Private theServiceController As ServiceController
Private Sub SetUpTimer()Sub SetUpTimer()
Try
theTimer = New Timers.Timer()
With theTimer
.AutoReset = True
.Interval = 5000
.Start()
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
Private Function CreateMenu()Function CreateMenu() As ContextMenuStrip
Dim mobContextMenu As New ContextMenuStrip
mobContextMenu.Items.Add("停止", Nothing, New EventHandler(AddressOf StopService))
mobContextMenu.Items.Add("暂停", Nothing, New EventHandler(AddressOf PauseService))
mobContextMenu.Items.Add("继续", Nothing, New EventHandler(AddressOf ContinueService))
mobContextMenu.Items.Add("开始", Nothing, New EventHandler(AddressOf StartService))
mobContextMenu.Items.Add("-")
mobContextMenu.Items.Add("关于", Nothing, New EventHandler(AddressOf AboutBox))
mobContextMenu.Items.Add("退出", Nothing, New EventHandler(AddressOf ExitController))
Return mobContextMenu
End Function
Private Sub GetServiceStatus()Sub GetServiceStatus()
Try
theServiceController.Refresh() '读取状态之前先进行刷新
'变更菜单项和图标
Select Case theServiceController.Status()
Case ServiceProcess.ServiceControllerStatus.Paused
theNotifyIcon.Icon = My.Resources.Resource1.Pause
theNotifyIcon.ContextMenuStrip.Items(0).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = True
theNotifyIcon.ContextMenuStrip.Items(3).Enabled = False
Case ServiceProcess.ServiceControllerStatus.Running
theNotifyIcon.Icon = My.Resources.Resource1.Run
theNotifyIcon.ContextMenuStrip.Items(0).Enabled = True
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = True
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(3).Enabled = False
Case ServiceProcess.ServiceControllerStatus.Stopped
theNotifyIcon.Icon = My.Resources.Resource1._Stop
theNotifyIcon.ContextMenuStrip.Items(0).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(3).Enabled = True
Case ServiceProcess.ServiceControllerStatus.ContinuePending, ServiceProcess.ServiceControllerStatus.PausePending, _
ServiceProcess.ServiceControllerStatus.StartPending, ServiceProcess.ServiceControllerStatus.StopPending
theNotifyIcon.Icon = My.Resources.Resource1.Pause
theNotifyIcon.ContextMenuStrip.Items(0).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(3).Enabled = False
End Select
'检查“暂停”和“继续”使用可用
If theServiceController.CanPauseAndContinue = False Then
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = False
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'停止服务的过程
Private Sub StopService()Sub StopService(ByVal sender As Object, ByVal e As EventArgs)
Try
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Running Then
If theServiceController.CanStop = True Then theServiceController.Stop()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'暂停服务的过程
Private Sub PauseService()Sub PauseService(ByVal sender As Object, ByVal e As EventArgs)
Try
If Not theServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then
If theServiceController.CanPauseAndContinue = True Then theServiceController.Pause()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'继续服务程序的过程
Private Sub ContinueService()Sub ContinueService(ByVal sender As Object, ByVal e As EventArgs)
Try
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then
If theServiceController.CanPauseAndContinue = True Then theServiceController.Continue()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'开始服务程序的过程
Private Sub StartService()Sub StartService(ByVal sender As Object, ByVal e As EventArgs)
Try
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
theServiceController.Start()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'“关于”菜单项的过程
Private Sub AboutBox()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 ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'退出服务程序的过程
Private Sub ExitController()Sub ExitController(ByVal sender As Object, ByVal e As EventArgs)
Try
With thetimer
.Stop()
.Dispose()
End With
With theNotifyIcon
.Visible = False
.Dispose()
.Dispose()
End With
Application.Exit()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'定时器停止
Public Sub mobTimer_Elapsed()Sub mobTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles theTimer.Elapsed
Try
GetServiceStatus()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'系统托盘图标单击事件
Public Sub mobNotifyIcon_Click()Sub mobNotifyIcon_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles theNotifyIcon.Click
'System.Diagnostics.Process.Start("IExplore.exe", "http://blog.csdn.net/wzuomin/")
End Sub
Public Sub Main()Sub Main()
Try
theServiceController = New System.ServiceProcess.ServiceController("System Service") '建立与服务程序的连接
theNotifyIcon = New NotifyIcon()
With theNotifyIcon
.Visible = False
.ContextMenuStrip = CreateMenu()
.Text = "【笔直的一道弯】" + Microsoft.VisualBasic.ChrW(10) + "http://blog.csdn.net/wzuomin/"
Call SetUpTimer()
.Visible = True
End With
Application.Run()
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error")
End
End Try
End Sub
End Module
Imports System.Diagnostics
Imports System.ServiceProcess
Module ModMain Module ModMain
Private WithEvents theNotifyIcon As NotifyIcon
Private WithEvents theTimer As Timers.Timer
Private theServiceController As ServiceController
Private Sub SetUpTimer()Sub SetUpTimer()
Try
theTimer = New Timers.Timer()
With theTimer
.AutoReset = True
.Interval = 5000
.Start()
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
Private Function CreateMenu()Function CreateMenu() As ContextMenuStrip
Dim mobContextMenu As New ContextMenuStrip
mobContextMenu.Items.Add("停止", Nothing, New EventHandler(AddressOf StopService))
mobContextMenu.Items.Add("暂停", Nothing, New EventHandler(AddressOf PauseService))
mobContextMenu.Items.Add("继续", Nothing, New EventHandler(AddressOf ContinueService))
mobContextMenu.Items.Add("开始", Nothing, New EventHandler(AddressOf StartService))
mobContextMenu.Items.Add("-")
mobContextMenu.Items.Add("关于", Nothing, New EventHandler(AddressOf AboutBox))
mobContextMenu.Items.Add("退出", Nothing, New EventHandler(AddressOf ExitController))
Return mobContextMenu
End Function
Private Sub GetServiceStatus()Sub GetServiceStatus()
Try
theServiceController.Refresh() '读取状态之前先进行刷新
'变更菜单项和图标
Select Case theServiceController.Status()
Case ServiceProcess.ServiceControllerStatus.Paused
theNotifyIcon.Icon = My.Resources.Resource1.Pause
theNotifyIcon.ContextMenuStrip.Items(0).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = True
theNotifyIcon.ContextMenuStrip.Items(3).Enabled = False
Case ServiceProcess.ServiceControllerStatus.Running
theNotifyIcon.Icon = My.Resources.Resource1.Run
theNotifyIcon.ContextMenuStrip.Items(0).Enabled = True
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = True
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(3).Enabled = False
Case ServiceProcess.ServiceControllerStatus.Stopped
theNotifyIcon.Icon = My.Resources.Resource1._Stop
theNotifyIcon.ContextMenuStrip.Items(0).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(3).Enabled = True
Case ServiceProcess.ServiceControllerStatus.ContinuePending, ServiceProcess.ServiceControllerStatus.PausePending, _
ServiceProcess.ServiceControllerStatus.StartPending, ServiceProcess.ServiceControllerStatus.StopPending
theNotifyIcon.Icon = My.Resources.Resource1.Pause
theNotifyIcon.ContextMenuStrip.Items(0).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(3).Enabled = False
End Select
'检查“暂停”和“继续”使用可用
If theServiceController.CanPauseAndContinue = False Then
theNotifyIcon.ContextMenuStrip.Items(1).Enabled = False
theNotifyIcon.ContextMenuStrip.Items(2).Enabled = False
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'停止服务的过程
Private Sub StopService()Sub StopService(ByVal sender As Object, ByVal e As EventArgs)
Try
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Running Then
If theServiceController.CanStop = True Then theServiceController.Stop()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'暂停服务的过程
Private Sub PauseService()Sub PauseService(ByVal sender As Object, ByVal e As EventArgs)
Try
If Not theServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then
If theServiceController.CanPauseAndContinue = True Then theServiceController.Pause()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'继续服务程序的过程
Private Sub ContinueService()Sub ContinueService(ByVal sender As Object, ByVal e As EventArgs)
Try
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then
If theServiceController.CanPauseAndContinue = True Then theServiceController.Continue()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'开始服务程序的过程
Private Sub StartService()Sub StartService(ByVal sender As Object, ByVal e As EventArgs)
Try
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
theServiceController.Start()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'“关于”菜单项的过程
Private Sub AboutBox()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 ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'退出服务程序的过程
Private Sub ExitController()Sub ExitController(ByVal sender As Object, ByVal e As EventArgs)
Try
With thetimer
.Stop()
.Dispose()
End With
With theNotifyIcon
.Visible = False
.Dispose()
.Dispose()
End With
Application.Exit()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'定时器停止
Public Sub mobTimer_Elapsed()Sub mobTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles theTimer.Elapsed
Try
GetServiceStatus()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'系统托盘图标单击事件
Public Sub mobNotifyIcon_Click()Sub mobNotifyIcon_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles theNotifyIcon.Click
'System.Diagnostics.Process.Start("IExplore.exe", "http://blog.csdn.net/wzuomin/")
End Sub
Public Sub Main()Sub Main()
Try
theServiceController = New System.ServiceProcess.ServiceController("System Service") '建立与服务程序的连接
theNotifyIcon = New NotifyIcon()
With theNotifyIcon
.Visible = False
.ContextMenuStrip = CreateMenu()
.Text = "【笔直的一道弯】" + Microsoft.VisualBasic.ChrW(10) + "http://blog.csdn.net/wzuomin/"
Call SetUpTimer()
.Visible = True
End With
Application.Run()
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error")
End
End Try
End Sub
End Module