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
<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-te2\'></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-te2\'></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-te2\'></fp> MsgBoxStyle<fp class=\'fp-fsvyp\'></fp>Critical) End End Try End Sub