wasSharpNET – Rev 27

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  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));
        }
    }
}

Generated by GNU Enscript 1.6.5.90.