예제 #1
0
        /// <summary>
        /// Starts the specified service
        /// </summary>
        /// <param name="ServiceName">The service name </param>
        /// <param name="TimeOutSec">Optionl Timout in seconds</param>
        /// <returns></returns>
        public static bool StartService(string ServiceName, int TimeOutSec = 180)
        {
            bool Exists = false;

            foreach (ServiceController SvcCtrl in ServiceController.GetServices())
            {
                if (SvcCtrl.ServiceName == ServiceName)
                {
                    Exists = true;
                    WriteToApplicationLog("Start Service: " + ServiceName, EventLogEntryType.Information);
                    try
                    {
                        if (SvcCtrl.Status == ServiceControllerStatus.Stopped)
                        {
                            SvcCtrl.Start();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                        }
                        else
                        {
                            throw new Exception(ServiceName + "Service is already running!");
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteToApplicationLog(ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error);
                        return(false);
                    }
                    WriteToApplicationLog("Service Started successfully: " + ServiceName, EventLogEntryType.SuccessAudit);
                    break;
                }
            }
            if (!Exists)
            {
                WriteToApplicationLog("Service Not Found: " + ServiceName, EventLogEntryType.SuccessAudit);
                throw new Exception("Service Not Found: " + ServiceName);
            }
            return(true);
        }
예제 #2
0
        /// <summary>
        /// Stops and restarts a specified service
        /// </summary>
        /// <param name="ServiceName">The service name </param>
        /// <param name="TimeOutSec">Optionl Timout in seconds</param>
        /// <returns></returns>
        public static bool BounceService(string ServiceName, int TimeOutSec = 180)
        {
            bool Exists = false;

            foreach (ServiceController SvcCtrl in ServiceController.GetServices())
            {
                if (SvcCtrl.ServiceName == ServiceName)
                {
                    Exists = true;
                    WriteToApplicationLog("Bouncing Service: " + ServiceName, EventLogEntryType.Information);
                    try
                    {
                        switch (SvcCtrl.Status)
                        {
                        case ServiceControllerStatus.Stopped:
                            SvcCtrl.Start();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            break;

                        case ServiceControllerStatus.StopPending:
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            SvcCtrl.Start();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            break;

                        case ServiceControllerStatus.PausePending:
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Paused, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            SvcCtrl.Stop();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            SvcCtrl.Start();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            break;

                        case ServiceControllerStatus.ContinuePending:
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            SvcCtrl.Stop();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            SvcCtrl.Start();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            break;

                        default:
                            SvcCtrl.Stop();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            SvcCtrl.Start();
                            SvcCtrl.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(TimeSpan.TicksPerSecond * TimeOutSec));
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteToApplicationLog(ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error);
                        return(false);
                    }
                    WriteToApplicationLog("Service restarted successfully: " + ServiceName, EventLogEntryType.SuccessAudit);
                    break;
                }
            }
            if (!Exists)
            {
                WriteToApplicationLog("Service Not Found: " + ServiceName, EventLogEntryType.SuccessAudit);
                throw new Exception("Service Not Found: " + ServiceName);
            }
            return(true);
        }