C # Developing Windows Service Program control features

Source: Internet
Author: User

In doing some of the planning tasks will inevitably use the Windows Service Service program, and this is no interface, each start, restart, shutdown requires the service interface to find services to operate, for ordinary people is very troublesome, so sometimes need to control windows through the application Service, put a service control class that was previously written to it.

C # Windows Service Control class code

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.ServiceProcess;usingSystem.threading;namespace tools.app{ Public classServicehelper {//   <summary>           //   Restart Windows service        //   </summary>           //   <param name= "ServiceName" > TheWindows service display name</param>           //   <returns> If The restart successfully return true else return false</ Returns>            Public Static BOOL Restartwindowsservice(stringServiceName) {BOOLBresult =false;Try{Try{Stopwindowsservice (serviceName); Thread.Sleep ( +); }Catch(Exception ex)                    {Startwindowsservice (serviceName); Thread.Sleep ( +);                    Stopwindowsservice (ServiceName); Thread.Sleep ( +); Console.WriteLine (ex.                Message); }Try{Startwindowsservice (serviceName); Thread.Sleep ( +); }Catch(Exception ex)                    {Stopwindowsservice (serviceName); Thread.Sleep ( +);                    Startwindowsservice (ServiceName); Thread.Sleep ( +); Console.WriteLine (ex.                Message); } bresult =true; }Catch(Exception ex) {bresult =false;ThrowEx }returnBresult; }//   <summary>           //   Start Windows service        //   </summary>           //   <param name= "ServiceName" > TheWindows service display name</param>           //   <returns>If the start successfully return true else return false</returns>            Public Static BOOL Stopwindowsservice(stringServiceName) {servicecontroller[] SCs = Servicecontroller.getservices ();BOOLBresult =false;foreach(ServiceController SCinchSCS) {if(SC. ServiceName = = ServiceName) {Try{SC. WaitForStatus (servicecontrollerstatus.running, Timespan.fromseconds ( +* -)); Sc.                        Stop (); Bresult =true; }Catch(Exception ex) {bresult =false;ThrowEx }                }            }returnBresult; }//   <summary>           ///   Stop Windows service        //   </summary>           //   <param name= "ServiceName" > TheWindows service display name</param>           //   <returns>If The stop successfully return true else return false</returns>            Public Static BOOL Startwindowsservice(stringServiceName) {servicecontroller[] SCs = Servicecontroller.getservices ();BOOLBresult =false;foreach(ServiceController SCinchSCS) {if(SC. ServiceName = = ServiceName) {Try{SC. WaitForStatus (servicecontrollerstatus.stopped, Timespan.fromseconds ( +* -)); Sc.                        Start (); Bresult =true; }Catch(Exception ex) {bresult =false;ThrowEx }                }            }returnBresult; } Public Static BOOL serviceisexisted(stringServiceName) {servicecontroller[] services = servicecontroller.getservices ();foreach(ServiceController sinchServices) {if(S.servicename = = ServiceName) {return true; }            }return false; }    }}

Form Button Event call code

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.ServiceProcess;usingSystem.Text;usingSystem.windows.forms;namespace tools.app{ Public Partial classServiceform:form { Public Serviceform() {InitializeComponent (); }//   <summary>         ///   load to determine if the service exists        //   </summary>         ///   <param name= "sender" ></param>         ///   <param name= "E" ></param>         Private void Serviceform_load(Objectsender, EventArgs e) {if(Servicehelper.serviceisexisted ("Tasktimer") {ServiceController Service =NewServiceController ("Tasktimer");if(Service. Status! = servicecontrollerstatus.stopped && service. Status! = servicecontrollerstatus.stoppending) { This. btnstart.enabled =false; This. button2. Enabled =true; }Else{ This. btnstart.enabled =true; This. button2. Enabled =false; }            }        }//   <summary>         //   start        //   </summary>         ///   <param name= "sender" ></param>         ///   <param name= "E" ></param>         Private void btnStart_Click(Objectsender, EventArgs e) {Servicehelper.startwindowsservice ("Tasktimer"); This. btnstart.enabled =false; This. button2. Enabled =true; MessageBox.Show ("Start Successful","Hint"); }//   <summary>         //   Close        //   </summary>         ///   <param name= "sender" ></param>         ///   <param name= "E" ></param>         Private void btnClose_Click(Objectsender, EventArgs e) {Servicehelper.stopwindowsservice ("Tasktimer"); This. btnstart.enabled =true; This. button2. Enabled =false; MessageBox.Show ("Close Success","Hint"); }Private void Btnisexisted_click(Objectsender, EventArgs e) {BOOLBL = servicehelper.serviceisexisted ("Tasktimer");if(BL) {MessageBox.Show ("Tasktimer exists!" ","Hint"); }Else{MessageBox.Show ("Tasktimer does not exist!" ","Hint"); }        }    }}

Hope that the above share of the beginner friends some help, thank you!
More Attention Fuyi Technology blog: Http://blog.csdn.net/fuyifang
or scan the QR code directly with your phone to see more Posts:

C # Developing Windows Service Program control features

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.