// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceProcess; using System.Threading; using System.Data; namespace Spludlow { public class ServiceProcesses { private static int WaitStartSeconds = 10; private static int WaitStopSeconds = 120; public static string StatusName(ServiceControllerStatus status) { return Enum.GetName(typeof(ServiceControllerStatus), status); } public static string Query(string serviceName) { using (ServiceController serviceController = new ServiceController(serviceName)) { return StatusName(serviceController.Status); } } public static DataSet QueryAll() { return Queries(null); } public static DataSet Queries(string[] serviceNames) { DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "Host Name Status", "String String String", }); if (serviceNames == null) { List serviceList = new List(); foreach (ServiceController serviceController in ServiceController.GetServices()) { serviceList.Add(serviceController.ServiceName); serviceController.Dispose(); } serviceNames = serviceList.ToArray(); } foreach (string serviceName in serviceNames) { using (ServiceController serviceController = new ServiceController(serviceName)) { string status = StatusName(serviceController.Status); table.Rows.Add(new object[] { Environment.MachineName, serviceController.ServiceName, status }); } } return Spludlow.Data.ADO.WireDataSet(table); } public static bool Exists(string serviceName) { List serviceList = new List(); foreach (ServiceController serviceController in ServiceController.GetServices()) { serviceList.Add(serviceController.ServiceName); serviceController.Dispose(); } return serviceList.Contains(serviceName); } public static void Start(string serviceName) { Start(new string[] { serviceName }, false); } public static void Start(string serviceName, bool dontWait) { Start(new string[] { serviceName }, dontWait); } public static void Start(string[] serviceNames) { Start(serviceNames, false); } public static void Start(string[] serviceNames, bool dontWait) { foreach (string serviceName in serviceNames) { using (ServiceController serviceController = new ServiceController(serviceName)) { if (serviceController.Status != ServiceControllerStatus.Stopped) continue; serviceController.Start(); if (dontWait == false) { try { serviceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(WaitStartSeconds)); } catch (System.ServiceProcess.TimeoutException ee) { throw new ApplicationException("Service Process; Starting Timeout:\t" + serviceName + ", " + StatusName(serviceController.Status), ee); } } } } } public static void Stop(string serviceName) { Stop(new string[] { serviceName }, false); } public static void Stop(string serviceName, bool dontWait) { Stop(new string[] { serviceName }, dontWait); } public static void Stop(string[] serviceNames) { Stop(serviceNames, false); } public static void Stop(string[] serviceNames, bool dontWait) { foreach (string serviceName in serviceNames) { using (ServiceController serviceController = new ServiceController(serviceName)) { if (serviceController.Status != ServiceControllerStatus.Running) continue; serviceController.Stop(); if (dontWait == false) { try { serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(WaitStopSeconds)); } catch (System.ServiceProcess.TimeoutException ee) { throw new ApplicationException("Service Process; Stopping Timeout:\t" + serviceName + ", " + StatusName(serviceController.Status), ee); } } } } } } }