如何控制Windows服务程序

自己编写程序来控制windows服务程序的启动、暂停、继续及退出。

Imports  System.Text
Imports  System.Diagnostics
Imports  System.ServiceProcess

Module ModMain
    
Private WithEvents theNotifyIcon As NotifyIcon
    
Private WithEvents theTimer As Timers.Timer
    
Private theServiceController As ServiceController

    
Private Sub SetUpTimer()
        
Try
            theTimer 
= New Timers.Timer()
            
With theTimer
                .AutoReset 
= True
                .Interval 
= 5000
                .Start()
            
End With
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
Private Function CreateMenu() As ContextMenuStrip
        
Dim mobContextMenu As New ContextMenuStrip
        mobContextMenu.Items.Add(
"停止"NothingNew EventHandler(AddressOf StopService))
        mobContextMenu.Items.Add(
"暂停"NothingNew EventHandler(AddressOf PauseService))
        mobContextMenu.Items.Add(
"继续"NothingNew EventHandler(AddressOf ContinueService))
        mobContextMenu.Items.Add(
"开始"NothingNew EventHandler(AddressOf StartService))
        mobContextMenu.Items.Add(
"-")
        mobContextMenu.Items.Add(
"关于"NothingNew EventHandler(AddressOf AboutBox))
        mobContextMenu.Items.Add(
"退出"NothingNew EventHandler(AddressOf ExitController))
        
Return mobContextMenu
    
End Function


    
Private Sub GetServiceStatus()
        
Try
            theServiceController.Refresh() 
'读取状态之前先进行刷新 
            '变更菜单项和图标 
            Select Case theServiceController.Status()
                
Case ServiceProcess.ServiceControllerStatus.Paused
                    theNotifyIcon.Icon 
= My.Resources.Resource1.Pause
                    theNotifyIcon.ContextMenuStrip.Items(
0).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
1).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
2).Enabled = True
                    theNotifyIcon.ContextMenuStrip.Items(
3).Enabled = False
                
Case ServiceProcess.ServiceControllerStatus.Running
                    theNotifyIcon.Icon 
= My.Resources.Resource1.Run
                    theNotifyIcon.ContextMenuStrip.Items(
0).Enabled = True
                    theNotifyIcon.ContextMenuStrip.Items(
1).Enabled = True
                    theNotifyIcon.ContextMenuStrip.Items(
2).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
3).Enabled = False
                
Case ServiceProcess.ServiceControllerStatus.Stopped
                    theNotifyIcon.Icon 
= My.Resources.Resource1._Stop
                    theNotifyIcon.ContextMenuStrip.Items(
0).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
1).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
2).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
3).Enabled = True
                
Case ServiceProcess.ServiceControllerStatus.ContinuePending, ServiceProcess.ServiceControllerStatus.PausePending, _
                     ServiceProcess.ServiceControllerStatus.StartPending, ServiceProcess.ServiceControllerStatus.StopPending
                    theNotifyIcon.Icon 
= My.Resources.Resource1.Pause
                    theNotifyIcon.ContextMenuStrip.Items(
0).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
1).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
2).Enabled = False
                    theNotifyIcon.ContextMenuStrip.Items(
3).Enabled = False
            
End Select
            
'检查“暂停”和“继续”使用可用 
            If theServiceController.CanPauseAndContinue = False Then
                theNotifyIcon.ContextMenuStrip.Items(
1).Enabled = False
                theNotifyIcon.ContextMenuStrip.Items(
2).Enabled = False
            
End If
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
'停止服务的过程 
    Private Sub StopService(ByVal sender As ObjectByVal e As EventArgs)
        
Try
            
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Running Then
                
If theServiceController.CanStop = True Then theServiceController.Stop()
            
End If
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
'暂停服务的过程 
    Private Sub PauseService(ByVal sender As ObjectByVal e As EventArgs)
        
Try
            
If Not theServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then
                
If theServiceController.CanPauseAndContinue = True Then theServiceController.Pause()
            
End If
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
'继续服务程序的过程 
    Private Sub ContinueService(ByVal sender As ObjectByVal e As EventArgs)
        
Try
            
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then
                
If theServiceController.CanPauseAndContinue = True Then theServiceController.Continue()
            
End If
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
'开始服务程序的过程 
    Private Sub StartService(ByVal sender As ObjectByVal e As EventArgs)
        
Try
            
If theServiceController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
                theServiceController.Start()
            
End If
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
'“关于”菜单项的过程 
    Private Sub AboutBox(ByVal sender As ObjectByVal e As EventArgs)
        
Try
            
Dim obStringBuilder As New StringBuilder()
            
With obStringBuilder
                .Append(
"Service Controller")
                .Append(vbCrLf)
                .Append(
"CLR 版本:")
                .Append(Environment.Version.ToString)
                
MsgBox(.ToString, MsgBoxStyle.Information)
            
End With
            obStringBuilder 
= Nothing
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
'退出服务程序的过程 
    Private Sub ExitController(ByVal sender As ObjectByVal e As EventArgs)
        
Try
            
With thetimer
                .Stop()
                .Dispose()
            
End With
            
With theNotifyIcon
                .Visible 
= False
                .Dispose()
                .Dispose()
            
End With
            Application.Exit()
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
'定时器停止 
    Public Sub mobTimer_Elapsed(ByVal sender As ObjectByVal e As System.Timers.ElapsedEventArgs) Handles theTimer.Elapsed
        
Try
            GetServiceStatus()
        
Catch ex As Exception
            
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        
End Try
    
End Sub


    
'系统托盘图标单击事件 
    Public Sub mobNotifyIcon_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles theNotifyIcon.Click
        
'System.Diagnostics.Process.Start("IExplore.exe", "http://blog.csdn.net/wzuomin/")
    End Sub


    
Public Sub Main()
        
Try
            theServiceController 
= New System.ServiceProcess.ServiceController("System Service"'建立与服务程序的连接 
            theNotifyIcon = New NotifyIcon()
            
With theNotifyIcon
                .Visible 
= False
                .ContextMenuStrip 
= CreateMenu()
                .Text 
= "【笔直的一道弯】" + Microsoft.VisualBasic.ChrW(10+ "http://blog.csdn.net/wzuomin/"
                
Call SetUpTimer()
                .Visible 
= True
            
End With
            Application.Run()
        
Catch ex As Exception
            
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error")
            
End
        
End Try
    
End Sub


End Module

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值