wasSharpNET

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 26  →  ?path2? @ 27
/Platform/Windows/Services/Utilities.cs
@@ -0,0 +1,46 @@
///////////////////////////////////////////////////////////////////////////
// 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
{
/// <summary>
/// Start or stop a service.
/// </summary>
/// <param name="service">the name of service</param>
/// <param name="running">the requested service state</param>
/// <returns>true if the action could be performed</returns>
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));
}
}
}