///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
using System;
using System.ServiceProcess;
namespace wasSharpNET.Platform.Windows.Services
{
public static class Utilities
{
///
/// Start or stop a service.
///
/// the name of service
/// the requested service state
/// true if the action could be performed
public static void SetServiceRunning(string service, bool running)
{
var s = new ServiceController(service);
// Do not make any change in case the service status matches the requested service status.
if (s.Status.Equals(ServiceControllerStatus.Running) && running)
return;
if (s.Status.Equals(ServiceControllerStatus.Stopped) && !running)
return;
switch (running)
{
case true:
s.Start();
break;
case false:
s.Stop();
break;
}
// Default Windows timespan of 30 seconds for service status result.
s.WaitForStatus(running ? ServiceControllerStatus.Running : ServiceControllerStatus.Stopped,
TimeSpan.FromSeconds(60));
}
}
}