Control Windows Services from Windows System Tray

Source: Internet
Author: User
Control Windows Services from Windows System Tray
Author: Meng xianhui from: [wonderful world of Meng xianhui] Release Date: 17:38:11

Control Windows Services from Windows System Tray

VB. NET has many built-in classes that allow us to easily create Windows service programs. But how can we easily control these services? Generally, it is controlled in the management tool. This article describes how to create a program running in the system tray to easily control a service program. For details about how to create a service program, refer to. Net SDK or other articles about how to create a service program. The example in this article uses the w3svc service of IIS as an example to control the stop and start of IIS.

To develop such a program, open Microsoft Visual Studio. net, create a solution named servicecontroller, create a visual basic Windows application named winform, and. net automatically created form1.vb deleted, because the application we created has no user interface, we run the program in sub main.

First add reference-. NET-System.ServiceProcess.dll, create a module named modmain, the Code is as follows:

Imports system. text <br/> imports system. diagnostics <br/> imports system. serviceprocess </P> <p> Public module modmain </P> <p> private withevents mob1_yicon as policyicon <br/> private withevents mobcontextmenu as contextmenu <br/> private withevents mobtimer timers. timer <br/> private mobservicecontroller as servicecontroller </P> <p> end module <br/>

The above code first references three namespaces, and then defines four variables: mobtyyicon will be displayed in the system tray; contextmenu will display menu information; mobtimer is the timer, check the service status to change the menu and graph status at any time; mobservicecontroller indicates a Windows service application and allows you to connect to a running or stopped service, operate on it, or obtain information about it.

Because the service program does not have a user interface, we set three icons to identify the service status. Here we have made three simple icons to identify the service status: running. ICO, paused. ICO, stopped. ICO:

Next we will establish the timer setuptimer process. Generally, the interval between IIS stop or startup is 5 seconds, and we will use 5 seconds for the timer interval. The Code is as follows:

Private sub setuptimer () <br/> try <br/> mobtimer = new timers. timer () <br/> with mobtimer <br/>. autoreset = true <br/>. interval = 5000 <br/>. start () <br/> end with <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub <br/>

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

Private sub createmenu () <br/> try <br/> mobcontextmenu. menuitems. add (New menuitem ("stop", new eventhandler (addressof stopservice) <br/> mobcontextmenu. menuitems. add (New menuitem ("Suspend", new eventhandler (addressof pauseservice) <br/> mobcontextmenu. menuitems. add (New menuitem ("continue", new eventhandler (addressof continueservice) <br/> mobcontextmenu. menuitems. add (New menuitem ("START", new eventhandler (addressof startservice) <br/> mobcontextmenu. menuitems. add ("-") <br/> mobcontextmenu. menuitems. add (New menuitem ("about", new eventhandler (addressof aboutbox) <br/> mobcontextmenu. menuitems. add (New menuitem ("exit", new eventhandler (addressof exitcontroller ))) <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub <br/>

When we change the running status of the service, we should reflect this change to the user. Here we use the icons of the pallets for different identification. When a service program starts, you must first establish the service status. First, the getservicestatus process calls the refresh method of servicecontroller, which will refresh all the attributes of servicecontroller. To accurately obtain the status of the service program, this process is crucial. The following select case statement creates different menu items and tray icons based on the status of different service programs.

Private sub getservicestatus () <br/> try <br/> '// refresh before reading the status <br/> mobservicecontroller. refresh () <br/> '// change menu items and icons <br/> select case mobservicecontroller. status () <br/> case serviceprocess. servicecontrollerstatus. paused <br/> mobpolicyicon. icon = new icon ("paused. ICO ") <br/> mobcontextmenu. menuitems (0 ). enabled = false <br/> mobcontextmenu. menuitems (1 ). enabled = false <br/> mobcontextmenu. menuitems (2 ). enabled = true <br/> mobcontextmenu. menuitems (3 ). enabled = false <br/> case serviceprocess. servicecontrollerstatus. running <br/> mobpolicyicon. icon = new icon ("running. ICO ") <br/> mobcontextmenu. menuitems (0 ). enabled = true <br/> mobcontextmenu. menuitems (1 ). enabled = true <br/> mobcontextmenu. menuitems (2 ). enabled = false <br/> mobcontextmenu. menuitems (3 ). enabled = false <br/> case serviceprocess. servicecontrollerstatus. stopped <br/> mobypyicon. icon = new icon ("stopped. ICO ") <br/> mobcontextmenu. menuitems (0 ). enabled = false <br/> mobcontextmenu. menuitems (1 ). enabled = false <br/> mobcontextmenu. menuitems (2 ). enabled = false <br/> mobcontextmenu. menuitems (3 ). enabled = true <br/> case _ <br/> serviceprocess. servicecontrollerstatus. continuepending, _ <br/> serviceprocess. servicecontrollerstatus. pausepending, _ <br/> serviceprocess. servicecontrollerstatus. startpending, _ <br/> serviceprocess. servicecontrollerstatus. stoppending <br/> mobpolicyicon. icon = new icon ("paused. ICO ") <br/> mobcontextmenu. menuitems (0 ). enabled = false <br/> mobcontextmenu. menuitems (1 ). enabled = false <br/> mobcontextmenu. menuitems (2 ). enabled = false <br/> mobcontextmenu. menuitems (3 ). enabled = false <br/> end select <br/> '// check whether "Suspend" and "continue" are available. <br/> If mobservicecontroller. canpauseandcontinue = false then <br/> mobcontextmenu. menuitems (1 ). enabled = false <br/> mobcontextmenu. menuitems (2 ). enabled = false <br/> end if </P> <p> catch obex as exception <br/> throw obex <br/> end try <br/> end sub <br />

Create an event handler for the menu item below:

'// Service stop Process <br/> private sub stopservice (byval sender as object, byval e as eventargs) <br/> try <br/> If mobservicecontroller. status = serviceprocess. servicecontrollerstatus. running then <br/> If mobservicecontroller. canstop = true then <br/> mobservicecontroller. stop () <br/> end if <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub </ p> <p> '// service suspension process <br/> private sub pauseservice (byval sender as object, byval e as eventargs) <br/> try <br/> if not mobservicecontroller. status = serviceprocess. servicecontrollerstatus. paused = true then <br/> If mobservicecontroller. canpauseandcontinue = true then <br/> mobservicecontroller. pause () <br/> end if <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub </ p> <p> '// the process of continuing the Service Program <br/> private sub continueservice (byval sender as object, byval e as eventargs) <br/> try <br/> If mobservicecontroller. status = serviceprocess. servicecontrollerstatus. paused = true then <br/> If mobservicecontroller. canpauseandcontinue = true then <br/> mobservicecontroller. continue () <br/> end if <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub </ p> <p> '// process of starting the Service Program <br/> private sub startservice (byval sender as object, byval e as eventargs) <br/> try <br/> If mobservicecontroller. status = serviceprocess. servicecontrollerstatus. stopped then <br/> mobservicecontroller. start () <br/> end if <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub </P> <p>' // "about" menu item Process <br/> private sub aboutbox (byval sender as object, byval e as eventargs) <br/> try <br/> dim obstringbuilder as new stringbuilder () <br/> with obstringbuilder <br/>. append ("service controller example") <br/>. append (vbcrlf) <br/>. append ("CLR version:") <br/>. append (environment. version. tostring) <br/> msgbox (. tostring, msgboxstyle. information) <br/> end with <br/> obstringbuilder = nothing <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub </P> <p> '// exit the service program process <br/> private sub exitcontroller (byval sender as object, byval e as eventargs) <br/> try <br/> mobtimer. stop () <br/> mobtimer. dispose () <br/> mobility yicon. visible = false <br/> mobility yicon. dispose () <br/> mobservicecontroller. dispose () <br/> application. exit () <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub </P> <p> '// stop the timer <br /> Public sub mobtimer_elapsed (byval sender as object, byval e as system. timers. elapsedeventargs) _ <br/> handles mobtimer. elapsed <br/> try <br/> getservicestatus () <br/> catch obex as exception <br/> throw obex <br/> end try <br/> end sub </P> <p> '// click the system tray icon event <br/> Public sub mobility yicon_click (byval sender as object, byval e as system. eventargs) _ <br/> handles mobility yicon. click <br/> system. diagnostics. process. start ("iexplore.exe", "http://xml.sz.luohuedu.net/") <br/> end sub <br/>

The main program is as follows:

Public sub main () <br/> try <br/> '// establish a connection with the Service Program <br/> mobservicecontroller = new system. serviceprocess. servicecontroller ("IISADMIN") <br/> '// hide the icon. After you know that the menu items and icons are ready. <Br/> mobypyicon = new notifyicon () <br/> mobypyicon. visible = false <br/> mobcontextmenu = new contextmenu () <br/> createmenu () <br/> mobtoffyicon. contextmenu = mobcontextmenu <br/> mobpolicyicon. TEXT = "[wonderful world of the mengxian meeting]" + _ <br/> Microsoft. visualBasic. chrw (10) + "http://xml.sz.luohuedu.net/" <br/> setuptimer () <br/> mobypyicon. visible = true <br/> application. run () <br/> catch obex as exception <br/> msgbox (obex. message. tostring, msgboxstyle. critical) <br/> end try <br/> end sub <br/>

The running result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.