Control Windows service from the Window system tray

zhaozj2021-02-11  174

From the Window system tray control of Windows Services: Meng will come from: [Meng] will be the best of the world Date: 2003-8-5 17:38:11

Controlling Windows Services VB.NET from the Window System Tray Many built-in classes, allowing us to easily create a Windows service, but how to easily control these services? Generally, it is controlled in the management tool. This article will describe how to create a program running in the system tray to easily control a service program. As for how to create a service program, you can refer to the article .NET SDK or other articles that create a service program, the examples of this article use IIS's W3SVC services to control the stop and start of IIS. To develop such a program, let's open Microsoft Visual Studio.net, create a new solution called ServiceController, then create a Windows application named WinForm's Visual Basic type, then automatically created Form1.vb created by VS.NET to delete Drop, because we created the app no ​​user interface, we run the program at the Sub Main. Add the reference to -.Net-System.ServiceProcess.dll, the new module named modmain, the code is as follows:

Imports System.Text Imports System.Diagnostics Imports System.ServiceProcess Public Module modMain Private WithEvents mobNotifyIcon As NotifyIcon Private WithEvents mobContextMenu As ContextMenu Private WithEvents mobTimer As Timers.Timer Private mobServiceController As ServiceController End Module

The above code first references three namespaces, then defined four variables: Mobnotifyicon will display in the system tray; ContextMenu Displays the menu information; MobTimer is the timer, the original check of the service, change the menu and icon at any time State; MobServiceController represents the Windows service application and allows you to operate or stop, and operate it or get information about it. Since the service program is no user interface, we set three icon identification services, here, do three simple icons to identify the status of service: Running.ico, Paused.ico, Stopped.ico, as follows: We set the timer setuptimer process, usually, the interval of IIS stops or started for 5 seconds, we use 5 seconds to do the timer's interval, the code is as follows:

Private sub setuptimer () TRY Mobtimer = new Timers.timer () with mobtimer .AUTORESET = true .interval = 5000.start () End with catch Obex as Exception throw Obex End Try End Sub

Below, create a context menu and add an event handler for each menu item:

Private Sub CreateMenu () Try mobContextMenu.MenuItems.Add (New MenuItem ( "Stop", New EventHandler (AddressOf StopService))) mobContextMenu.MenuItems.Add (New MenuItem ( "pause", New EventHandler (AddressOf PauseService))) mobContextMenu. MenuItems.Add (New MenuItem ( "continue", New EventHandler (AddressOf ContinueService))) mobContextMenu.MenuItems.Add (New MenuItem ( "start", New EventHandler (AddressOf StartService))) mobContextMenu.MenuItems.Add ( "-") mobContextMenu.MenuItems.Add (New MenuItem ( "on", New EventHandler (AddressOf AboutBox))) mobContextMenu.MenuItems.Add (New MenuItem ( "exit", New EventHandler (AddressOf ExitController))) Catch obEx As Exception Throw obEx End Try End Sub When we change the service's operating state, we should reflect this change to the user, where the icon is used to identify. When the service program starts, the status of the service is to be established. First, the GetServiceStatus process calls the serviceController's Refresh method, which will refresh the serviceController all properties. To accurately obtain the status of the service, this process is critical. The following SELECT CASE statement creates different menu items and tray icons according to the status of different servers.

'Before the first refresh mobServiceController.Refresh // read status ()' Private Sub GetServiceStatus () Try // change menu items and icons Select Case mobServiceController.Status () Case ServiceProcess.ServiceControllerStatus.Paused mobNotifyIcon.Icon = New Icon ( " Paused.ico ") mobContextMenu.MenuItems (0) .Enabled = False mobContextMenu.MenuItems (1) .Enabled = False mobContextMenu.MenuItems (2) .Enabled = True mobContextMenu.MenuItems (3) .Enabled = False Case ServiceProcess.ServiceControllerStatus. Running mobNotifyIcon.Icon = New Icon ( "Running.ico") mobContextMenu.MenuItems (0) .Enabled = True mobContextMenu.MenuItems (1) .Enabled = True mobContextMenu.MenuItems (2) .Enabled = False mobContextMenu.MenuItems (3) .Enabled = False Case ServiceProcess.ServiceControllerStatus.Stopped mobNotifyIcon.Icon = New Icon ( "Stopped.ico") mobContextMenu.MenuItems (0) .Enabled = False mobContextMenu.MenuItems (1) .Enabled = False mobContextMenu.MenuItems (2). Enabled = false mobcontextMenu.Menuitems (3) .enabled = true case _ serviceProcess.serviceControllerstatus.Contin uePending, _ ServiceProcess.ServiceControllerStatus.PausePending, _ ServiceProcess.ServiceControllerStatus.StartPending, _ ServiceProcess.ServiceControllerStatus.StopPending mobNotifyIcon.Icon = New Icon ( "Paused.ico") mobContextMenu.MenuItems (0) .Enabled = False mobContextMenu.MenuItems (1 ) .Enabled = False mobContextMenu.MenuItems (2) .Enabled = False mobContextMenu.MenuItems (3) .Enabled = False End Select '// check "pause" and "continue" using available If mobServiceController.CanPauseAndContinue = False Then mobContextMenu.MenuItems (1) .enabled = false mobcontextMenu.Menuitems (2) .enabled =

False End If Catch Obex AS Exception Throw Obex End End Sub Event Handle The Event Handle of the Menu item is created:

'// stop the service process Private Sub StopService (ByVal sender As Object, ByVal e As EventArgs) Try If mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Running Then If mobServiceController.CanStop = True Then mobServiceController.Stop () End If End If Catch obEx As Exception Throw obEx End Try End Sub '// suspended process Private Sub PauseService (ByVal sender As Object, ByVal e As EventArgs) Try If Not mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Paused = True Then If mobServiceController.CanPauseAndContinue = true Then mobServiceController.Pause () process End If End If Catch obEx As Exception Throw obEx End Try End Sub '// continue service program Private Sub ContinueService (ByVal sender As Object, ByVal e As EventArgs) Try If mobServiceController.Status = ServiceProcess .ServiceControllerStatus.Paused = True Then If mobServiceController.CanPauseAndContinue = True Then mobServiceController.Continue () procedure End If End If Catch obEx As Exception Throw obEx End Try End Sub '// start service routine Private Sub St artService (ByVal sender As Object, ByVal e As EventArgs) Try If mobServiceController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then mobServiceController.Start () End If Catch obEx As Exception Throw obEx End Try End Sub '// "About" menu item process Private Sub AboutBox (ByVal sender As Object, ByVal e As EventArgs) Try Dim obStringBuilder As New StringBuilder () With obStringBuilder .Append ( "Service Controller uses examples") .Append (vbCrLf) .Append ( "CLR version:"). Append (Environment.Version.toString) msgbox (.tostring, msgboxstyle.information) end with ibstringbuilder = nothing catch Obex as exception throw Obex end e

// Exit the routine procedure Private Sub ExitController (ByVal sender As Object, ByVal e As EventArgs) Try mobTimer.Stop () mobTimer.Dispose () mobNotifyIcon.Visible = False mobNotifyIcon.Dispose () mobServiceController.Dispose () Application.Exit () Catch obEx As Exception Throw obEx End Try End Sub '// stop the timer Public Sub mobTimer_Elapsed (ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) _ Handles mobTimer.Elapsed Try GetServiceStatus () Catch obEx As Exception Throw obEx End Try End Sub '// click the system tray icon events Public Sub mobNotifyIcon_Click (ByVal sender As Object, ByVal e As System.EventArgs) _ Handles mobNotifyIcon.Click System.Diagnostics.Process.Start ( "IExplore.exe", " Http://xml.sz.luohuedu.net/ ") End Sub is the main program:

Public Sub Main () Try '// Establish a connection with the service program MobServiceController = new system.serviceProcess.ServiceController ("iisadmin")' // Hide icon, know the menu item, and the graph standard is ready. mobNotifyIcon = "[Meng will be a wonderful World]" New NotifyIcon () mobNotifyIcon.Visible = False mobContextMenu = New ContextMenu () CreateMenu () mobNotifyIcon.ContextMenu = mobContextMenu mobNotifyIcon.Text = _ Microsoft.VisualBasic.ChrW (10) "http://xml.sz.luohuedu.net/" setuptimer () mobnotifyicon.visible = true application.run () catch Obex as exception msgbox (obex.Message.toString, msgboxstyle.critical) End Try End Sub

The results of the operation are as follows:

转载请注明原文地址:https://www.9cbs.com/read-5411.html

New Post(0)