124 lines
5.6 KiB
C#
124 lines
5.6 KiB
C#
|
using AppServerWatchDogService.Enums;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Linq;
|
|||
|
using System.ServiceProcess;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace AppServerWatchDogService
|
|||
|
{
|
|||
|
public class ServiceControlUtils
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Start a service by it's name
|
|||
|
/// </summary>
|
|||
|
/// <param name="ServiceName"></param>
|
|||
|
public static void startService(string ServiceName)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
ServiceController sc = new ServiceController();
|
|||
|
sc.ServiceName = ServiceName;
|
|||
|
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("The {0} service status is currently set to {1}", ServiceName, sc.Status.ToString()), EventLogEntryType.Information, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
|
|||
|
if (sc.Status == ServiceControllerStatus.Stopped)
|
|||
|
{
|
|||
|
// Start the service if the current status is stopped.
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("Starting the {0} service ...", ServiceName), EventLogEntryType.Information, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
try
|
|||
|
{
|
|||
|
// Start the service, and wait until its status is "Running".
|
|||
|
sc.Start();
|
|||
|
sc.WaitForStatus(ServiceControllerStatus.Running);
|
|||
|
|
|||
|
// Display the current service status.
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("The {0} service status is now set to {1}.", ServiceName, sc.Status.ToString()), EventLogEntryType.Information, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
}
|
|||
|
catch (InvalidOperationException e)
|
|||
|
{
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("Could not start the {0} service." + Environment.NewLine + e.ToString(), ServiceName), EventLogEntryType.Error, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("Service {0} already running.", ServiceName), EventLogEntryType.Warning, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("Could not start the {0} service. Because: " + ex.ToString(), ServiceName), EventLogEntryType.Error, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Stop a service that is active
|
|||
|
/// </summary>
|
|||
|
/// <param name="ServiceName"></param>
|
|||
|
public static void stopService(string ServiceName)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
ServiceController sc = new ServiceController();
|
|||
|
sc.ServiceName = ServiceName;
|
|||
|
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("The {0} service status is currently set to {1}", ServiceName, sc.Status.ToString()), EventLogEntryType.Information, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
|
|||
|
if (sc.Status == ServiceControllerStatus.Running)
|
|||
|
{
|
|||
|
// Start the service if the current status is stopped.
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("Stopping the {0} service ...", ServiceName), EventLogEntryType.Information, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
try
|
|||
|
{
|
|||
|
// Start the service, and wait until its status is "Running".
|
|||
|
sc.Stop();
|
|||
|
sc.WaitForStatus(ServiceControllerStatus.Stopped);
|
|||
|
|
|||
|
// Display the current service status.
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("The {0} service status is now set to {1}.", ServiceName, sc.Status.ToString()), EventLogEntryType.Information, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
}
|
|||
|
catch (InvalidOperationException e)
|
|||
|
{
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("Could not stop the {0} service." + Environment.NewLine + e.ToString(), ServiceName), EventLogEntryType.Error, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("Cannot stop service {0} because it's already inactive.", ServiceName), EventLogEntryType.Warning, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
AppServerWatchDogService.WriteEventLog(Program.COMPANY, String.Format("Could not stop the {0} service. Because: " + ex.ToString(), ServiceName), EventLogEntryType.Error, (Int32)EventId.EVENT_WATCHDOG);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static bool IsServiceRunning(string ServiceName)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
ServiceController sc = new ServiceController();
|
|||
|
sc.ServiceName = ServiceName;
|
|||
|
|
|||
|
if (sc.Status == ServiceControllerStatus.Running)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|