熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

從Window系統托盤控制Windows服務

2022-06-13   來源: .NET編程 

  有許多內建的類可以讓我們輕松創建Windows服務程序但如何輕松控制這些服務呢?一般是到管理工具裡面進行控制本文將描述如何創建一個運行在系統托盤裡的程序來輕松控制一個服務程序至於如何創建服務程序可以參考NET SDK或其它創建服務程序的文章本文的例子利用IIS的WSVC服務來做例子來控制IIS的停止與啟動

  要開發這樣的程序我們先打開Microsoft Visual StudioNET新建一個名為ServiceController的解決方案然後新建名為WinForm的Visual Basic類型的Windows應用程序然後把自動創建的Formvb刪除掉因為我們創建的應用程序沒有用戶界面我們在Sub Main運行程序

  先添加引用-NET-SystemServiceProcessdll新建名為modMain的模塊代碼如下

Imports System<fp class='fp-ax2rz'></fp>Text Imports System<fp class='fp-e4fvj'></fp>Diagnostics Imports System<fp class='fp-e4fvj'></fp>ServiceProcess Public Module modMain Private WithEvents mobNotifyIcon As NotifyIcon Private WithEvents mobContextMenu As ContextMenu Private WithEvents mobTimer As Timers<fp class='fp-fsvyp'></fp>Timer Private mobServiceController As ServiceController End Module

  上面的代碼首先引用了三個名稱空間然後分別定義了四個變量mobNotifyIcon將會在系統托盤裡顯示ContextMenu顯示菜單信息mobTimer為定時器原來檢查服務的狀態以隨時變更菜單和圖標的狀態mobServiceController表示Windows服務應用程序並允許連接到正在運行或者已停止的服務對其進行操作或獲取有關它的信息

  由於服務程序是沒有用戶界面的因此我們設置三種圖標標識服務的狀態這裡做了三個簡單的圖標來標識服務的狀態RunningicoPausedicoStoppedico分別如下

  

  下面我們就建立定時器SetUpTimer過程通常IIS停止或啟動的間隔為我們就用秒來做定時器的間隔代碼如下

Private Sub SetUpTimer() Try mobTimer = New Timers<fp class='fp-e4fvj'></fp>Timer() With mobTimer <fp class='fp-ax2rz'></fp>AutoReset = True <fp class='fp-fsvyp'></fp>Interval = <fp class='fp-r5vde'></fp><fp class='fp-z7ief'></fp><fp class='fp-z7ief'></fp><fp class='fp-d9zwb'></fp> <fp class='fp-5qsob'></fp>Start() End With Catch obEx As Exception Throw obEx End Try End Sub

  下面創建上下文菜單的過程並為每個菜單項添加事件處理程序

Private Sub CreateMenu() Try mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems<fp class='fp-e4fvj'></fp>Add(New MenuItem(<fp class='fp-rt4bw'></fp>停止<fp class='fp-rt4bw'></fp><fp class='fp-9eeua'></fp>New EventHandler(AddressOf StopService))) mobContextMenu<fp class='fp-5qsob'></fp>MenuItems<fp class='fp-fsvyp'></fp>Add(New MenuItem(<fp class='fp-rt4bw'></fp>暫停<fp class='fp-323hg'></fp><fp class='fp-thke2'></fp>New EventHandler(AddressOf PauseService))) mobContextMenu<fp class='fp-e4fvj'></fp>MenuItems<fp class='fp-5qsob'></fp>Add(New MenuItem(<fp class='fp-exfzh'></fp>繼續<fp class='fp-sob6f'></fp><fp class='fp-a2uwg'></fp>New EventHandler(AddressOf ContinueService))) mobContextMenu<fp class='fp-fsvyp'></fp>MenuItems<fp class='fp-ax2rz'></fp>Add(New MenuItem(<fp class='fp-sob6f'></fp>開始<fp class='fp-323hg'></fp><fp class='fp-thke2'></fp>New EventHandler(AddressOf StartService))) mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems<fp class='fp-5qsob'></fp>Add(<fp class='fp-rt4bw'></fp><fp class='fp-naabd'></fp><fp class='fp-sob6f'></fp>) mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems<fp class='fp-fsvyp'></fp>Add(New MenuItem(<fp class='fp-exfzh'></fp>關於<fp class='fp-rt4bw'></fp><fp class='fp-mmu87'></fp>New EventHandler(AddressOf AboutBox))) mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems<fp class='fp-ax2rz'></fp>Add(New MenuItem(<fp class='fp-rt4bw'></fp>退出<fp class='fp-323hg'></fp><fp class='fp-a2uwg'></fp>New EventHandler(AddressOf ExitController))) Catch obEx As Exception Throw obEx End Try End Sub

  當我們改變了服務的運行狀態時我們應當向用戶反映這一變化這裡用托盤的圖標不同來進行標識當服務程序啟動時就要先建立服務的狀態首先GetServiceStatus過程調用ServiceController的Refresh方法它將會刷新的ServiceController所有屬性要准確得到服務程序的狀態這一過程是至關重要的下面的Select Case語句根據不同的服務程序的狀態建立不同的菜單項和托盤圖標

Private Sub GetServiceStatus() Try <fp class='fp-yj1b9'></fp>//讀取狀態之前先進行刷新 mobServiceController<fp class='fp-ax2rz'></fp>Refresh() <fp class='fp-pty82'></fp>//變更菜單項和圖標 Select Case mobServiceController<fp class='fp-5qsob'></fp>Status() Case ServiceProcess<fp class='fp-5qsob'></fp>ServiceControllerStatus<fp class='fp-fsvyp'></fp>Paused mobNotifyIcon<fp class='fp-5qsob'></fp>Icon = New Icon(<fp class='fp-sob6f'></fp>Paused<fp class='fp-5qsob'></fp>ico<fp class='fp-rt4bw'></fp>) mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems(<fp class='fp-clqyu'></fp>)<fp class='fp-e4fvj'></fp>Enabled = False mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems(<fp class='fp-qhfb8'></fp>)<fp class='fp-fsvyp'></fp>Enabled = False mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems(<fp class='fp-zo2mf'></fp>)<fp class='fp-fsvyp'></fp>Enabled = True mobContextMenu<fp class='fp-fsvyp'></fp>MenuItems(<fp class='fp-474rm'></fp>)<fp class='fp-e4fvj'></fp>Enabled = False Case ServiceProcess<fp class='fp-e4fvj'></fp>ServiceControllerStatus<fp class='fp-ax2rz'></fp>Running mobNotifyIcon<fp class='fp-e4fvj'></fp>Icon = New Icon(<fp class='fp-sob6f'></fp>Running<fp class='fp-5qsob'></fp>ico<fp class='fp-exfzh'></fp>) mobContextMenu<fp class='fp-5qsob'></fp>MenuItems(<fp class='fp-z7ief'></fp>)<fp class='fp-ax2rz'></fp>Enabled = True mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems(<fp class='fp-qhfb8'></fp>)<fp class='fp-e4fvj'></fp>Enabled = True mobContextMenu<fp class='fp-fsvyp'></fp>MenuItems(<fp class='fp-9ldqc'></fp>)<fp class='fp-ax2rz'></fp>Enabled = False mobContextMenu<fp class='fp-5qsob'></fp>MenuItems(<fp class='fp-474rm'></fp>)<fp class='fp-fsvyp'></fp>Enabled = False Case ServiceProcess<fp class='fp-e4fvj'></fp>ServiceControllerStatus<fp class='fp-fsvyp'></fp>Stopped mobNotifyIcon<fp class='fp-e4fvj'></fp>Icon = New Icon(<fp class='fp-323hg'></fp>Stopped<fp class='fp-ax2rz'></fp>ico<fp class='fp-exfzh'></fp>) mobContextMenu<fp class='fp-5qsob'></fp>MenuItems(<fp class='fp-z7ief'></fp>)<fp class='fp-fsvyp'></fp>Enabled = False mobContextMenu<fp class='fp-fsvyp'></fp>MenuItems(<fp class='fp-qhfb8'></fp>)<fp class='fp-e4fvj'></fp>Enabled = False mobContextMenu<fp class='fp-ax2rz'></fp>MenuItems(<fp class='fp-u51g8'></fp>)<fp class='fp-ax2rz'></fp>Enabled = False mobContextMenu<fp class='fp-fsvyp'></fp>MenuItems(<fp class='fp-flw1n'></fp>)<fp class='fp-e4fvj'></fp>Enabled = True Case _ ServiceProcess<fp class='fp-fsvyp'></fp>ServiceControllerStatus<fp class='fp-fsvyp'></fp>ContinuePending<fp class='fp-a2uwg'></fp> _ ServiceProcess<fp class='fp-5qsob'></fp>ServiceControllerStatus<fp class='fp-5qsob'></fp>PausePending<fp class='fp-9eeua'></fp> _ ServiceProcess<fp class='fp-5qsob'></fp>ServiceControllerStatus<fp class='fp-e4fvj'></fp>StartPending<fp class='fp-thke2'></fp> _ ServiceProcess<fp class='fp-e4fvj'></fp>ServiceControllerStatus<fp class='fp-fsvyp'></fp>StopPending mobNotifyIcon<fp class='fp-ax2rz'></fp>Icon = New Icon(<fp class='fp-323hg'></fp>Paused<fp class='fp-e4fvj'></fp>ico<fp class='fp-sob6f'></fp>) mobContextMenu<fp class='fp-e4fvj'></fp>MenuItems(<fp class='fp-kax3v'></fp>)<fp class='fp-ax2rz'></fp>Enabled = False mobContextMenu<fp class='fp-e4fvj'></fp>MenuItems(<fp class='fp-blaph'></fp>)<fp class='fp-e4fvj'></fp>Enabled = False mobContextMenu<fp class='fp-fsvyp'></fp>MenuItems(<fp class='fp-zo2mf'></fp>)<fp class='fp-ax2rz'></fp>Enabled = False mobContextMenu<fp class='fp-fsvyp'></fp>MenuItems(<fp class='fp-474rm'></fp>)<fp class='fp-ax2rz'></fp>Enabled = False End Select <fp class='fp-yj1b9'></fp>//檢查<fp class='fp-dx7ut'></fp>暫停<fp class='fp-2qegt'></fp>和<fp class='fp-ouq3e'></fp>繼續<fp class='fp-jgh4o'></fp>使用可用 If mobServiceController<fp class='fp-5qsob'></fp>CanPauseAndContinue = False Then mobContextMenu<fp class='fp-5qsob'></fp>MenuItems(<fp class='fp-42bl5'></fp>)<fp class='fp-ax2rz'></fp>Enabled = False mobContextMenu<fp class='fp-5qsob'></fp>MenuItems(<fp class='fp-zo2mf'></fp>)<fp class='fp-ax2rz'></fp>Enabled = False End If Catch obEx As Exception Throw obEx End Try End Sub

  下面建立菜單項的事件處理程序:

<fp class='fp-k4x6u'></fp>//停止服務的過程 Private Sub StopService(ByVal sender As Object<fp class='fp-mmu87'></fp> ByVal e As EventArgs) Try If mobServiceController<fp class='fp-ax2rz'></fp>Status = ServiceProcess<fp class='fp-5qsob'></fp>ServiceControllerStatus<fp class='fp-fsvyp'></fp>Running Then If mobServiceController<fp class='fp-fsvyp'></fp>CanStop = True Then mobServiceController<fp class='fp-5qsob'></fp>Stop() End If End If Catch obEx As Exception Throw obEx End Try End Sub <fp class='fp-yj1b9'></fp>//暫停服務的過程 Private Sub PauseService(ByVal sender As Object<fp class='fp-a2uwg'></fp> ByVal e As EventArgs) Try If Not mobServiceController<fp class='fp-ax2rz'></fp>Status = ServiceProcess<fp class='fp-ax2rz'></fp>ServiceControllerStatus<fp class='fp-ax2rz'></fp>Paused = True Then If mobServiceController<fp class='fp-ax2rz'></fp>CanPauseAndContinue = True Then mobServiceController<fp class='fp-e4fvj'></fp>Pause() End If End If Catch obEx As Exception Throw obEx End Try End Sub <fp class='fp-pty82'></fp>//繼續服務程序的過程 Private Sub ContinueService(ByVal sender As Object<fp class='fp-mmu87'></fp> ByVal e As EventArgs) Try If mobServiceController<fp class='fp-e4fvj'></fp>Status = ServiceProcess<fp class='fp-ax2rz'></fp>ServiceControllerStatus<fp class='fp-ax2rz'></fp>Paused = True Then If mobServiceController<fp class='fp-e4fvj'></fp>CanPauseAndContinue = True Then mobServiceController<fp class='fp-e4fvj'></fp>Continue() End If End If Catch obEx As Exception Throw obEx End Try End Sub <fp class='fp-ks64c'></fp>//開始服務程序的過程 Private Sub StartService(ByVal sender As Object<fp class='fp-a2uwg'></fp> ByVal e As EventArgs) Try If mobServiceController<fp class='fp-fsvyp'></fp>Status = ServiceProcess<fp class='fp-e4fvj'></fp>ServiceControllerStatus<fp class='fp-ax2rz'></fp>Stopped Then mobServiceController<fp class='fp-fsvyp'></fp>Start() End If Catch obEx As Exception Throw obEx End Try End Sub <fp class='fp-pty82'></fp>//<fp class='fp-07qj4'></fp>關於<fp class='fp-jgh4o'></fp>菜單項的過程 Private Sub AboutBox(ByVal sender As Object<fp class='fp-thke2'></fp> ByVal e As EventArgs) Try Dim obStringBuilder As New StringBuilder() With obStringBuilder <fp class='fp-5qsob'></fp>Append(<fp class='fp-rt4bw'></fp>Service Controller 使用例子<fp class='fp-sob6f'></fp>) <fp class='fp-ax2rz'></fp>Append(vbCrLf) <fp class='fp-fsvyp'></fp>Append(<fp class='fp-rt4bw'></fp>CLR 版本<fp class='fp-f94z5'></fp><fp class='fp-rt4bw'></fp>) <fp class='fp-fsvyp'></fp>Append(Environment<fp class='fp-ax2rz'></fp>Version<fp class='fp-ax2rz'></fp>ToString) MsgBox(<fp class='fp-ax2rz'></fp>ToString<fp class='fp-9eeua'></fp> MsgBoxStyle<fp class='fp-ax2rz'></fp>Information) End With obStringBuilder = Nothing Catch obEx As Exception Throw obEx End Try End Sub <fp class='fp-pty82'></fp>//退出服務程序的過程 Private Sub ExitController(ByVal sender As Object<fp class='fp-thke2'></fp> ByVal e As EventArgs) Try mobTimer<fp class='fp-ax2rz'></fp>Stop() mobTimer<fp class='fp-5qsob'></fp>Dispose() mobNotifyIcon<fp class='fp-5qsob'></fp>Visible = False mobNotifyIcon<fp class='fp-fsvyp'></fp>Dispose() mobServiceController<fp class='fp-ax2rz'></fp>Dispose() Application<fp class='fp-5qsob'></fp>Exit() Catch obEx As Exception Throw obEx End Try End Sub <fp class='fp-yj1b9'></fp>//定時器停止 Public Sub mobTimer_Elapsed(ByVal sender As Object<fp class='fp-9eeua'></fp> ByVal e As System<fp class='fp-fsvyp'></fp>Timers<fp class='fp-e4fvj'></fp>ElapsedEventArgs) _ Handles mobTimer<fp class='fp-5qsob'></fp>Elapsed Try GetServiceStatus() Catch obEx As Exception Throw obEx End Try End Sub <fp class='fp-k4x6u'></fp>//系統托盤圖標單擊事件 Public Sub mobNotifyIcon_Click(ByVal sender As Object<fp class='fp-9eeua'></fp> ByVal e As System<fp class='fp-ax2rz'></fp>EventArgs) _ Handles mobNotifyIcon<fp class='fp-5qsob'></fp>Click System<fp class='fp-fsvyp'></fp>Diagnostics<fp class='fp-e4fvj'></fp>Process<fp class='fp-5qsob'></fp>Start(<fp class='fp-rt4bw'></fp>IExplore<fp class='fp-5qsob'></fp>exe<fp class='fp-323hg'></fp><fp class='fp-mmu87'></fp> <fp class='fp-323hg'></fp><fp class='fp-exfzh'></fp>) End Sub

  下面就是主程序

Public Sub Main() Try <fp class='fp-yj1b9'></fp>//建立與服務程序的連接 mobServiceController = New System<fp class='fp-fsvyp'></fp>ServiceProcess<fp class='fp-e4fvj'></fp>ServiceController(<fp class='fp-rt4bw'></fp>IISAdmin<fp class='fp-exfzh'></fp>) <fp class='fp-k4x6u'></fp>//隱藏圖標<fp class='fp-n6918'></fp>知道菜單項和圖標准備好以後<fp class='fp-vopex'></fp> mobNotifyIcon = New NotifyIcon() mobNotifyIcon<fp class='fp-fsvyp'></fp>Visible = False mobContextMenu = New ContextMenu() CreateMenu() mobNotifyIcon<fp class='fp-5qsob'></fp>ContextMenu = mobContextMenu mobNotifyIcon<fp class='fp-5qsob'></fp>Text = <fp class='fp-323hg'></fp>【孟憲會之精彩世界】<fp class='fp-323hg'></fp> + _ Microsoft<fp class='fp-fsvyp'></fp>VisualBasic<fp class='fp-ax2rz'></fp>ChrW(<fp class='fp-blaph'></fp><fp class='fp-d9zwb'></fp>) + <fp class='fp-rt4bw'></fp><fp class='fp-rt4bw'></fp> SetUpTimer() mobNotifyIcon<fp class='fp-ax2rz'></fp>Visible = True Application<fp class='fp-fsvyp'></fp>Run() Catch obEx As Exception MsgBox(obEx<fp class='fp-5qsob'></fp>Message<fp class='fp-5qsob'></fp>ToString<fp class='fp-thke2'></fp> MsgBoxStyle<fp class='fp-fsvyp'></fp>Critical) End End Try End Sub

  運行結果如下

  


From:http://tw.wingwit.com/Article/program/net/201311/11472.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.