设为首页 收藏本站
查看: 945|回复: 0

[经验分享] 从Window系统托盘控制Windows服务

[复制链接]

尚未签到

发表于 2016-5-21 06:49:54 | 显示全部楼层 |阅读模式
从Window系统托盘控制Windows服务
作者: 孟宪会 出自: 【孟宪会之精彩世界】 发布日期: 2003-8-5 17:38:11
<!---->从Window系统托盘控制Windows服务   VB.NET有许多内建的类,可以让我们轻松创建Windows服务程序,但如何轻松控制这些服务呢?一般是到管理工具里面进行控制。本文将描述如何创建一个运行在系统托盘里的程序来轻松控制一个服务程序。至于如何创建服务程序,可以参考.NET SDK或其它创建服务程序的文章,本文的例子利用IIS的W3SVC服务来做例子,来控制IIS的停止与启动。
  要开发这样的程序,我们先打开Microsoft Visual Studio.NET,新建一个名为ServiceController的解决方案,然后新建名为WinForm的Visual Basic类型的Windows应用程序,然后把VS.NET自动创建的Form1.vb删除掉,因为我们创建的应用程序没有用户界面,我们在Sub Main运行程序。
  先添加引用-.NET-System.ServiceProcess.dll,新建名为modMain的模块,代码如下:
<xmp>Imports System.TextImports System.DiagnosticsImports System.ServiceProcessPublic Module modMainPrivate WithEvents mobNotifyIcon As NotifyIconPrivate WithEvents mobContextMenu As ContextMenuPrivate WithEvents mobTimer As Timers.TimerPrivate mobServiceController As ServiceControllerEnd Module</xmp>  上面的代码首先引用了三个名称空间,然后分别定义了四个变量:mobNotifyIcon将会在系统托盘里显示;ContextMenu显示菜单信息;mobTimer为定时器,原来检查服务的状态,以随时变更菜单和图标的状态;mobServiceController表示Windows服务应用程序并允许连接到正在运行或者已停止的服务、对其进行操作或获取有关它的信息。
  由于服务程序是没有用户界面的,因此我们设置三种图标标识服务的状态,这里做了三个简单的图标来标识服务的状态:Running.ico,Paused.ico,Stopped.ico,分别如下:
http://java2000-net.iyunv.com/Develop/ArticleImages/20/20312/CSDN_Dev_Image_2003-8-71759161.gif
  下面我们就建立定时器SetUpTimer过程,通常,IIS停止或启动的间隔为5秒,我们就用5秒来做定时器的间隔,代码如下:
<xmp>Private Sub SetUpTimer()TrymobTimer = New Timers.Timer()With mobTimer.AutoReset = True.Interval = 5000.Start()End WithCatch obEx As ExceptionThrow obExEnd TryEnd Sub</xmp>  下面,创建上下文菜单的过程,并为每个菜单项添加事件处理程序:
<xmp>Private Sub CreateMenu()TrymobContextMenu.MenuItems.Add(New MenuItem("停止",New EventHandler(AddressOf StopService)))mobContextMenu.MenuItems.Add(New MenuItem("暂停",New EventHandler(AddressOf PauseService)))mobContextMenu.MenuItems.Add(New MenuItem("继续",New EventHandler(AddressOf ContinueService)))mobContextMenu.MenuItems.Add(New MenuItem("开始",New EventHandler(AddressOf StartService)))mobContextMenu.MenuItems.Add("-")mobContextMenu.MenuItems.Add(New MenuItem("关于",New EventHandler(AddressOf AboutBox)))mobContextMenu.MenuItems.Add(New MenuItem("退出",New EventHandler(AddressOf ExitController)))Catch obEx As ExceptionThrow obExEnd TryEnd Sub</xmp>  当我们改变了服务的运行状态时,我们应当向用户反映这一变化,这里用托盘的图标不同来进行标识。当服务程序启动时,就要先建立服务的状态,首先GetServiceStatus过程调用ServiceController的Refresh方法,它将会刷新的ServiceController所有属性。要准确得到服务程序的状态,这一过程是至关重要的,下面的Select Case语句根据不同的服务程序的状态,建立不同的菜单项和托盘图标。
<xmp>Private Sub GetServiceStatus()Try'//读取状态之前先进行刷新mobServiceController.Refresh()'//变更菜单项和图标Select Case mobServiceController.Status()Case ServiceProcess.ServiceControllerStatus.PausedmobNotifyIcon.Icon = New Icon("Paused.ico")mobContextMenu.MenuItems(0).Enabled = FalsemobContextMenu.MenuItems(1).Enabled = FalsemobContextMenu.MenuItems(2).Enabled = TruemobContextMenu.MenuItems(3).Enabled = FalseCase ServiceProcess.ServiceControllerStatus.RunningmobNotifyIcon.Icon = New Icon("Running.ico")mobContextMenu.MenuItems(0).Enabled = TruemobContextMenu.MenuItems(1).Enabled = TruemobContextMenu.MenuItems(2).Enabled = FalsemobContextMenu.MenuItems(3).Enabled = FalseCase ServiceProcess.ServiceControllerStatus.StoppedmobNotifyIcon.Icon = New Icon("Stopped.ico")mobContextMenu.MenuItems(0).Enabled = FalsemobContextMenu.MenuItems(1).Enabled = FalsemobContextMenu.MenuItems(2).Enabled = FalsemobContextMenu.MenuItems(3).Enabled = TrueCase _ServiceProcess.ServiceControllerStatus.ContinuePending, _ServiceProcess.ServiceControllerStatus.PausePending, _ServiceProcess.ServiceControllerStatus.StartPending, _ServiceProcess.ServiceControllerStatus.StopPendingmobNotifyIcon.Icon = New Icon("Paused.ico")mobContextMenu.MenuItems(0).Enabled = FalsemobContextMenu.MenuItems(1).Enabled = FalsemobContextMenu.MenuItems(2).Enabled = FalsemobContextMenu.MenuItems(3).Enabled = FalseEnd Select'//检查“暂停”和“继续”使用可用If mobServiceController.CanPauseAndContinue = False ThenmobContextMenu.MenuItems(1).Enabled = FalsemobContextMenu.MenuItems(2).Enabled = FalseEnd IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub</xmp>  下面建立菜单项的事件处理程序:
<xmp>'//停止服务的过程Private Sub StopService(ByVal sender As Object, ByVal e As EventArgs)TryIf mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Running ThenIf mobServiceController.CanStop = True ThenmobServiceController.Stop()End IfEnd IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub'//暂停服务的过程Private Sub PauseService(ByVal sender As Object, ByVal e As EventArgs)TryIf Not mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True ThenIf mobServiceController.CanPauseAndContinue = True ThenmobServiceController.Pause()End IfEnd IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub'//继续服务程序的过程Private Sub ContinueService(ByVal sender As Object, ByVal e As EventArgs)TryIf mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True ThenIf mobServiceController.CanPauseAndContinue = True ThenmobServiceController.Continue()End IfEnd IfCatch obEx As ExceptionThrow obExEnd TryEnd Sub'//开始服务程序的过程Private Sub StartService(ByVal sender As Object, ByVal e As EventArgs)TryIf mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Stopped ThenmobServiceController.Start()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 obStringBuilder.Append("Service Controller 使用例子").Append(vbCrLf).Append("CLR 版本:").Append(Environment.Version.ToString)MsgBox(.ToString, MsgBoxStyle.Information)End WithobStringBuilder = NothingCatch obEx As ExceptionThrow obExEnd TryEnd Sub'//退出服务程序的过程Private Sub ExitController(ByVal sender As Object, ByVal e As EventArgs)TrymobTimer.Stop()mobTimer.Dispose()mobNotifyIcon.Visible = FalsemobNotifyIcon.Dispose()mobServiceController.Dispose()Application.Exit()Catch obEx As ExceptionThrow obExEnd TryEnd Sub'//定时器停止Public Sub mobTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) _Handles mobTimer.ElapsedTryGetServiceStatus()Catch obEx As ExceptionThrow obExEnd TryEnd Sub'//系统托盘图标单击事件Public Sub mobNotifyIcon_Click(ByVal sender As Object, ByVal e As System.EventArgs) _Handles mobNotifyIcon.ClickSystem.Diagnostics.Process.Start("IExplore.exe", "http://xml.sz.luohuedu.net/")End Sub</xmp>  下面就是主程序:
<xmp>Public Sub Main()Try'//建立与服务程序的连接mobServiceController = New System.ServiceProcess.ServiceController("IISAdmin")'//隐藏图标,知道菜单项和图标准备好以后。mobNotifyIcon = New NotifyIcon()mobNotifyIcon.Visible = FalsemobContextMenu = New ContextMenu()CreateMenu()mobNotifyIcon.ContextMenu = mobContextMenumobNotifyIcon.Text = "【孟宪会之精彩世界】" + _Microsoft.VisualBasic.ChrW(10) + "http://xml.sz.luohuedu.net/"SetUpTimer()mobNotifyIcon.Visible = TrueApplication.Run()Catch obEx As ExceptionMsgBox(obEx.Message.ToString, MsgBoxStyle.Critical)EndEnd TryEnd Sub</xmp>  运行结果如下:
http://java2000-net.iyunv.com/Develop/ArticleImages/20/20312/CSDN_Dev_Image_2003-8-71759163.gif

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-219665-1-1.html 上篇帖子: windows操作系统中SID是什么?? 下篇帖子: 关闭Windows 文件保护的方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表