有许多内建的类可以让我们轻松创建Windows服务程序但如何轻松控制这些服务呢?一般是到管理工具里面进行控制本文将描述如何创建一个运行在系统托盘里的程序来轻松控制一个服务程序至于如何创建服务程序可以参考NET SDK或其它创建服务程序的文章本文的例子利用IIS的WSVC服务来做例子来控制IIS的停止与启动 要开发这样的程序我们先打开Microsoft Visual StudioNET新建一个名为ServiceController的解决方案然后新建名为WinForm的Visual Basic类型的Windows应用程序然后把自动创建的Formvb删除掉因为我们创建的应用程序没有用户界面我们在Sub Main运行程序 先添加引用-NET-SystemServiceProcessdll新建名为modMain的模块代码如下 Imports SystemTextImports SystemDiagnosticsImports SystemServiceProcessPublic Module modMain Private WithEvents mobNotifyIcon As NotifyIcon Private WithEvents mobContextMenu As ContextMenu Private WithEvents mobTimer As TimersTimer Private mobServiceController As ServiceControllerEnd Module上面的代码首先引用了三个名称空间然后分别定义了四个变量mobNotifyIcon将会在系统托盘里显示ContextMenu显示菜单信息mobTimer为定时器原来检查服务的状态以随时变更菜单和图标的状态mobServiceController表示Windows服务应用程序并允许连接到正在运行或者已停止的服务对其进行操作或获取有关它的信息 由于服务程序是没有用户界面的因此我们设置三种图标标识服务的状态这里做了三个简单的图标来标识服务的状态RunningicoPausedicoStoppedico分别如下 下面我们就建立定时器SetUpTimer过程通常IIS停止或启动的间隔为秒我们就用秒来做定时器的间隔代码如下 Private Sub SetUpTimer()TrymobTimer = New TimersTimer()With mobTimerAutoReset = TrueInterval = Start()End WithCatch obEx As ExceptionThrow obExEnd TryEnd Sub下面创建上下文菜单的过程并为每个菜单项添加事件处理程序 Private Sub CreateMenu()TrymobContextMenuMenuItemsAdd(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 ExceptionThrow obExEnd TryEnd 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 IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub下面建立菜单项的事件处理程序: //停止服务的过程Private Sub StopService(ByVal sender As Object ByVal e As EventArgs)TryIf mobServiceControllerStatus = ServiceProcessServiceControllerStatusRunning ThenIf mobServiceControllerCanStop = True ThenmobServiceControllerStop()End IfEnd IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub//暂停服务的过程Private Sub PauseService(ByVal sender As Object ByVal e As EventArgs)TryIf Not mobServiceControllerStatus = ServiceProcessServiceControllerStatusPaused = True ThenIf mobServiceControllerCanPauseAndContinue = True ThenmobServiceControllerPause()End IfEnd IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub//继续服务程序的过程Private Sub ContinueService(ByVal sender As Object ByVal e As EventArgs)TryIf mobServiceControllerStatus = ServiceProcessServiceControllerStatusPaused = True ThenIf mobServiceControllerCanPauseAndContinue = True ThenmobServiceControllerContinue()End IfEnd IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub//开始服务程序的过程Private Sub StartService(ByVal sender As Object ByVal e As EventArgs)TryIf mobServiceControllerStatus = ServiceProcessServiceControllerStatusStopped ThenmobServiceControllerStart()End IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub//关于菜单项的过程Private Sub AboutBox(ByVal sender As Object ByVal e As EventArgs)TryDim obStringBuilder As New StringBuilder()With obStringBuilderAppend(Service Controller 使用例子)Append(vbCrLf)Append(CLR 版本)Append(EnvironmentVersionToString)MsgBox(ToString MsgBoxStyleInformation)End WithobStringBuilder = NothingCatch obEx As ExceptionThrow obExEnd TryEnd Sub//退出服务程序的过程Private Sub ExitController(ByVal sender As Object ByVal e As EventArgs)TrymobTimerStop()mobTimerDispose()mobNotifyIconVisible = FalsemobNotifyIconDispose()mobServiceControllerDispose()ApplicationExit()Catch obEx As ExceptionThrow obExEnd TryEnd Sub//定时器停止Public Sub mobTimer_Elapsed(ByVal sender As Object ByVal e As SystemTimersElapsedEventArgs) _Handles mobTimerElapsedTryGetServiceStatus()Catch obEx As ExceptionThrow obExEnd TryEnd Sub//系统托盘图标单击事件Public Sub mobNotifyIcon_Click(ByVal sender As Object ByVal e As SystemEventArgs) _Handles mobNotifyIconClickSystemDiagnosticsProcessStart(IExploreexe )End Sub下面就是主程序 Public Sub Main()Try//建立与服务程序的连接mobServiceController = New SystemServiceProcessServiceController(IISAdmin)//隐藏图标知道菜单项和图标准备好以后mobNotifyIcon = New NotifyIcon()mobNotifyIconVisible = FalsemobContextMenu = New ContextMenu()CreateMenu()mobNotifyIconContextMenu = mobContextMenumobNotifyIconText = 【孟宪会之精彩世界】 + _MicrosoftVisualBasicChrW() + SetUpTimer()mobNotifyIconVisible = TrueApplicationRun()Catch obEx As ExceptionMsgBox(obExMessageToString MsgBoxStyleCritical)EndEnd TryEnd Sub运行结果如下 |