有許多內建的類可以讓我們輕松創建Windows服務程序但如何輕松控制這些服務呢?一般是到管理工具裡面進行控制本文將描述如何創建一個運行在系統托盤裡的程序來輕松控制一個服務程序至於如何創建服務程序可以參考NET SDK或其它創建服務程序的文章本文的例子利用IIS的WSVC服務來做例子來控制IIS的停止與啟動
要開發這樣的程序我們先打開Microsoft Visual StudioNET新建一個名為ServiceController的解決方案然後新建名為WinForm的Visual Basic類型的Windows應用程序然後把自動創建的Formvb刪除掉因為我們創建的應用程序沒有用戶界面我們在Sub Main運行程序
先添加引用-NET-SystemServiceProcessdll新建名為modMain的模塊代碼如下
Imports SystemText
Imports SystemDiagnostics
Imports SystemServiceProcess
Public Module modMain
Private WithEvents mobNotifyIcon As NotifyIcon
Private WithEvents mobContextMenu As ContextMenu
Private WithEvents mobTimer As TimersTimer
Private mobServiceController As ServiceController
End Module
上面的代碼首先引用了三個名稱空間然後分別定義了四個變量mobNotifyIcon將會在系統托盤裡顯示ContextMenu顯示菜單信息mobTimer為定時器原來檢查服務的狀態以隨時變更菜單和圖標的狀態mobServiceController表示Windows服務應用程序並允許連接到正在運行或者已停止的服務對其進行操作或獲取有關它的信息
由於服務程序是沒有用戶界面的因此我們設置三種圖標標識服務的狀態這裡做了三個簡單的圖標來標識服務的狀態RunningicoPausedicoStoppedico分別如下
下面我們就建立定時器SetUpTimer過程通常IIS停止或啟動的間隔為秒我們就用秒來做定時器的間隔代碼如下
Private Sub SetUpTimer()
Try
mobTimer = New TimersTimer()
With mobTimer
AutoReset = True
Interval =
Start()
End With
Catch obEx As Exception
Throw obEx
End Try
End Sub
下面創建上下文菜單的過程並為每個菜單項添加事件處理程序
Private Sub CreateMenu()
Try
mobContextMenuMenuItemsAdd(New MenuItem(停止New EventHandler(AddressOf StopService)))
mobContextMenuMenuItemsAdd(New MenuItem(暫停New EventHandler(AddressOf PauseService)))
mobContextMenuMenuItemsAdd(New MenuItem(繼續New EventHandler(AddressOf ContinueService)))
mobContextMenuMenuItemsAdd(New MenuItem(開始New EventHandler(AddressOf StartService)))
mobContextMenuMenuItemsAdd()
mobContextMenuMenuItemsAdd(New MenuItem(關於New EventHandler(AddressOf AboutBox)))
mobContextMenuMenuItemsAdd(New MenuItem(退出New EventHandler(AddressOf ExitController)))
Catch obEx As Exception
Throw obEx
End Try
End Sub
當我們改變了服務的運行狀態時我們應當向用戶反映這一變化這裡用托盤的圖標不同來進行標識當服務程序啟動時就要先建立服務的狀態首先GetServiceStatus過程調用ServiceController的Refresh方法它將會刷新的ServiceController所有屬性要准確得到服務程序的狀態這一過程是至關重要的下面的Select Case語句根據不同的服務程序的狀態建立不同的菜單項和托盤圖標
Private Sub GetServiceStatus()
Try
//讀取狀態之前先進行刷新
mobServiceControllerRefresh()
//變更菜單項和圖標
Select Case mobServiceControllerStatus()
Case ServiceProcessServiceControllerStatusPaused
mobNotifyIconIcon = New Icon(Pausedico)
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = True
mobContextMenuMenuItems()Enabled = False
Case ServiceProcessServiceControllerStatusRunning
mobNotifyIconIcon = New Icon(Runningico)
mobContextMenuMenuItems()Enabled = True
mobContextMenuMenuItems()Enabled = True
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = False
Case ServiceProcessServiceControllerStatusStopped
mobNotifyIconIcon = New Icon(Stoppedico)
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = True
Case _
ServiceProcessServiceControllerStatusContinuePending _
ServiceProcessServiceControllerStatusPausePending _
ServiceProcessServiceControllerStatusStartPending _
ServiceProcessServiceControllerStatusStopPending
mobNotifyIconIcon = New Icon(Pausedico)
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()Enabled = False
End Select
//檢查暫停和繼續使用可用
If mobServiceControllerCanPauseAndContinue = False Then
mobContextMenuMenuItems()Enabled = False
mobContextMenuMenuItems()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 mobServiceControllerStatus = ServiceProcessServiceControllerStatusRunning Then
If mobServiceControllerCanStop = True Then
mobServiceControllerStop()
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 mobServiceControllerStatus = ServiceProcessServiceControllerStatusPaused = True Then
If mobServiceControllerCanPauseAndContinue = True Then
mobServiceControllerPause()
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 mobServiceControllerStatus = ServiceProcessServiceControllerStatusPaused = True Then
If mobServiceControllerCanPauseAndContinue = True Then
mobServiceControllerContinue()
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 mobServiceControllerStatus = ServiceProcessServiceControllerStatusStopped Then
mobServiceControllerStart()
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(EnvironmentVersionToString)
MsgBox(ToString MsgBoxStyleInformation)
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)
Try
mobTimerStop()
mobTimerDispose()
mobNotifyIconVisible = False
mobNotifyIconDispose()
mobServiceControllerDispose()
ApplicationExit()
Catch obEx As Exception
Throw obEx
End Try
End Sub
//定時器停止
Public Sub mobTimer_Elapsed(ByVal sender As Object ByVal e As SystemTimersElapsedEventArgs) _
Handles mobTimerElapsed
Try
GetServiceStatus()
Catch obEx As Exception
Throw obEx
End Try
End Sub
//系統托盤圖標單擊事件
Public Sub mobNotifyIcon_Click(ByVal sender As Object ByVal e As SystemEventArgs) _
Handles mobNotifyIconClick
SystemDiagnosticsProcessStart(IExploreexe )
End Sub
下面就是主程序
Public Sub Main()
Try
//建立與服務程序的連接
mobServiceController = New SystemServiceProcessServiceController(IISAdmin)
//隱藏圖標知道菜單項和圖標准備好以後
mobNotifyIcon = New NotifyIcon()
mobNotifyIconVisible = False
mobContextMenu = New ContextMenu()
CreateMenu()
mobNotifyIconContextMenu = mobContextMenu
mobNotifyIconText = 【孟憲會之精彩世界】 + _
MicrosoftVisualBasicChrW() +
SetUpTimer()
mobNotifyIconVisible = True
ApplicationRun()
Catch obEx As Exception
MsgBox(obExMessageToString MsgBoxStyleCritical)
End
End Try
End Sub
運行結果如下
From:http://tw.wingwit.com/Article/program/net/201311/11472.html