104 lines
2.9 KiB
C#
104 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.ServiceProcess;
|
|
using System.Text;
|
|
using System.Runtime.InteropServices;
|
|
using System.Timers;
|
|
|
|
namespace Watchdog
|
|
{
|
|
|
|
|
|
public partial class Watchdog : ServiceBase
|
|
{
|
|
public enum ServiceState
|
|
{
|
|
SERVICE_STOPPED = 0x00000001,
|
|
SERVICE_START_PENDING = 0x00000002,
|
|
SERVICE_STOP_PENDING = 0x00000003,
|
|
SERVICE_RUNNING = 0x00000004,
|
|
SERVICE_CONTINUE_PENDING = 0x00000005,
|
|
SERVICE_PAUSE_PENDING = 0x00000006,
|
|
SERVICE_PAUSED = 0x00000007,
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct ServiceStatus
|
|
{
|
|
public int dwServiceType;
|
|
public ServiceState dwCurrentState;
|
|
public int dwControlsAccepted;
|
|
public int dwWin32ExitCode;
|
|
public int dwServiceSpecificExitCode;
|
|
public int dwCheckPoint;
|
|
public int dwWaitHint;
|
|
};
|
|
|
|
private EventLog eventLog=null;
|
|
private Timer timer=null;
|
|
private int eventId=1;
|
|
public Watchdog()
|
|
{
|
|
InitializeComponent();
|
|
|
|
eventLog = new System.Diagnostics.EventLog();
|
|
if (!System.Diagnostics.EventLog.SourceExists("WatchdogSource"))
|
|
{
|
|
System.Diagnostics.EventLog.CreateEventSource(
|
|
"WatchdogSource","WatchdogLog");
|
|
}
|
|
eventLog.Source = "WatchdogSource";
|
|
eventLog.Log = "WatchdogLog";
|
|
}
|
|
[DllImport("advapi32.dll", SetLastError = true)]
|
|
private static extern bool SetServiceStatus(System.IntPtr handle, ref ServiceStatus serviceStatus);
|
|
protected override void OnStart(string[] args)
|
|
{
|
|
ServiceStatus serviceStatus = new ServiceStatus();
|
|
serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
|
|
serviceStatus.dwWaitHint = 100000;
|
|
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
|
|
|
|
eventLog.WriteEntry("Watchdog OnStop");
|
|
|
|
timer = new Timer();
|
|
timer.Interval = 1000; // 60 seconds
|
|
timer.Elapsed += new ElapsedEventHandler(this.OnTimer);
|
|
timer.Start();
|
|
|
|
serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
|
|
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
|
|
}
|
|
|
|
protected override void OnStop()
|
|
{
|
|
ServiceStatus serviceStatus = new ServiceStatus();
|
|
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOP_PENDING;
|
|
serviceStatus.dwWaitHint = 100000;
|
|
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
|
|
|
|
eventLog.WriteEntry("Watchdog OnStop");
|
|
|
|
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED;
|
|
SetServiceStatus(this.ServiceHandle, ref serviceStatus);
|
|
}
|
|
protected override void OnContinue()
|
|
{
|
|
eventLog.WriteEntry("Watchdog OnContinue");
|
|
}
|
|
|
|
private void OnTimer(Object sender,ElapsedEventArgs elapsedEventArgs)
|
|
{
|
|
if(1==eventId)timer.Interval=60000;
|
|
eventLog.WriteEntry(String.Format("Watchdog monitoring: {0}",eventId++));
|
|
List<ManagedProcess> managedProcessList=null;
|
|
managedProcessList=ProcessManager.GetManagedProceses();
|
|
ProcessManager.ManageProcesses(managedProcessList, eventLog);
|
|
|
|
}
|
|
}
|
|
}
|