C # Windows Services: Some methods (start and stop)

Source: Internet
Author: User

The previous section describes how to create and install a service (create a windows Service). The following describes how to start, stop, and determine whether to start the service. [Csharp] /// <summary> /// determine whether a service is installed /// </summary> /// <param name = "serviceName"> </param> /// <returns> </returns> public static bool ISWindowsServiceInstalled (string serviceName) {try {ServiceController [] services = ServiceController. getServices (); foreach (ServiceController service in services) {if (service. serviceName = serviceName) {return true;} return false;} catch {return false ;}} [csharp // <summary> /// start a service /// </summary> /// <param name = "serviceName"> </param> public static void StartService (string serviceName) {try {ServiceController [] services = ServiceController. getServices (); foreach (ServiceController service in services) {if (service. serviceName = serviceName) {service. start (); service. waitForStatus (ServiceControllerStatus. running, new TimeSpan (0, 0, 30 ));}}} catch {} [csharp] // <summary> // stop a service // </summary> // <param name = "serviceName"> </param> public static void StopService (string serviceName) {try {ServiceController [] services = ServiceController. getServices (); foreach (ServiceController service in services) {if (service. serviceName = serviceName) {service. stop (); service. waitForStatus (ServiceControllerStatus. running, new TimeSpan (0, 0, 30 ));}}} catch {}} [csharp] /// <summary> /// determine whether a service is started // </summary> /// <param name = "serviceName"> </param> public static bool ISStart (string serviceName) {bool result = true; try {ServiceController [] services = ServiceController. getServices (); foreach (ServiceController service in services) {if (service. serviceName = serviceName) {if (service. status = ServiceControllerStatus. stopped) | (service. status = ServiceControllerStatus. stopPending) {result = false ;}}} catch {} return result ;}

Related Article

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.