C # develop the Windows Service program control function,
The Windows Service Program is inevitably used for some scheduled tasks, but there is no operation interface. Every time you start, restart, or close a task, you need to find the Service on the Service interface for operations, it is very troublesome for ordinary people, so sometimes we need to control Windows Services through applications. Here we will post a previously written service control class.
C # Windows Service Control Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceProcess;using System.Threading;namespace Tools.App{ public class ServiceHelper { /// <summary> /// Restart windows service /// </summary> /// <param name="serviceName">the windows service display name</param> /// <returns> If the restart successfully return true else return false</returns> public static bool RestartWindowsService(string serviceName) { bool bResult = false; try { try { StopWindowsService(serviceName); Thread.Sleep(1000); } catch (Exception ex) { StartWindowsService(serviceName); Thread.Sleep(1000); StopWindowsService(serviceName); Thread.Sleep(1000); Console.WriteLine(ex.Message); } try { StartWindowsService(serviceName); Thread.Sleep(1000); } catch (Exception ex) { StopWindowsService(serviceName); Thread.Sleep(1000); StartWindowsService(serviceName); Thread.Sleep(1000); Console.WriteLine(ex.Message); } bResult = true; } catch (Exception ex) { bResult = false; throw ex; } return bResult; } /// <summary> /// Start windows service /// </summary> /// <param name="serviceName">the windows service display name</param> /// <returns>If the start successfully return true else return false</returns> public static bool StopWindowsService(string serviceName) { ServiceController[] scs = ServiceController.GetServices(); bool bResult = false; foreach (ServiceController sc in scs) { if (sc.ServiceName == serviceName) { try { sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(1000 * 30)); sc.Stop(); bResult = true; } catch (Exception ex) { bResult = false; throw ex; } } } return bResult; } /// <summary> /// Stop windows service /// </summary> /// <param name="serviceName">the windows service display name</param> /// <returns>If the stop successfully return true else return false</returns> public static bool StartWindowsService(string serviceName) { ServiceController[] scs = ServiceController.GetServices(); bool bResult = false; foreach (ServiceController sc in scs) { if (sc.ServiceName == serviceName) { try { sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(1000 * 30)); sc.Start(); bResult = true; } catch (Exception ex) { bResult = false; throw ex; } } } return bResult; } public static bool ServiceIsExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == serviceName) { return true; } } return false; } }}
Form button event call code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. serviceProcess; using System. text; using System. windows. forms; namespace Tools. app {public partial class ServiceForm: Form {public ServiceForm () {InitializeComponent ();} /// <summary> /// load to determine whether the service exists /// </summary> /// <param name = "sender"> </param> // <par Am name = "e"> </param> private void ServiceForm_Load (object sender, EventArgs e) {if (ServiceHelper. serviceIsExisted ("TaskTimer") {ServiceController service = new ServiceController ("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 (object sender, eventArgs e) {ServiceHelper. startWindowsService ("TaskTimer"); this. btnStart. enable D = false; this. button2.Enabled = true; MessageBox. show ("started successfully", "prompt ");} /// <summary> /// close /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void btnClose_Click (object sender, eventArgs e) {ServiceHelper. stopWindowsService ("TaskTimer"); this. btnStart. enabled = true; this. button2.Enabled = false; MessageBox. show ("disabled successfully", "prompt");} private void btnIsExisted_Clic K (object sender, EventArgs e) {bool bl = ServiceHelper. ServiceIsExisted ("TaskTimer"); if (bl) {MessageBox. Show ("TaskTimer exists! "," Prompt ");} else {MessageBox. Show (" TaskTimer does not exist! "," Prompt ");}}}}
I hope the above information will be helpful to beginners. Thank you!
More follow Fu Yi Fang technology blog: http://blog.csdn.net/fuyifang
Or scan the QR code on your mobile phone to view more blog posts: