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 { /// /// Restart windows service /// ///the windows service display name ///
If the restart successfully return true else return false
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; } /// /// Start windows service /// ///the windows service display name ///
If the start successfully return true else return false
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; } /// /// Stop windows service /// ///the windows service display name ///
If the stop successfully return true else return false
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 ();}////// Load to determine whether the service exists /////////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 ;}}}////// Start /////////Private void btnStart_Click (object sender, EventArgs e) {ServiceHelper. startWindowsService (TaskTimer); this. btnStart. enabled = false; this. button2.Enabled = true; MessageBox. show (startup successful, prompt );}////// Close /////////Private void btnClose_Click (object sender, EventArgs e) {ServiceHelper. stopWindowsService (TaskTimer); this. btnStart. enabled = true; this. button2.Enabled = false; MessageBox. show (close successful, prompt);} private void btnIsExisted_Click (object sender, EventArgs e) {bool bl = ServiceHelper. serviceIsExisted (TaskTimer); if (bl) {MessageBox. show (TaskTimer exists !, Prompt);} else {MessageBox. Show (TaskTimer does not exist !, Prompt );}}}}