using System; using System.Runtime.InteropServices; using System.ServiceProcess; namespace WorkhorseWhip { class Whip : ServiceBase { //DLL def for calling the threading method [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); [Flags] public enum EXECUTION_STATE : uint { ES_SYSTEM_REQUIRED = 0x00000001, ES_DISPLAY_REQUIRED = 0x00000002, // ES_USER_PRESENT = 0x00000004, ES_CONTINUOUS = 0x80000000 } static void Main(string[] args) { ServiceBase.Run(new Whip()); } public Whip() { this.ServiceName = "Workhorse Whip"; this.EventLog.Log = "Application"; // These Flags set whether or not to handle that specific // type of event. Set to true if you need it, false otherwise. this.CanHandlePowerEvent = false; this.CanHandleSessionChangeEvent = false; this.CanPauseAndContinue = true; this.CanShutdown = true; this.CanStop = true; } private void snap() { SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); } private void unSnap() { SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS); } protected override void OnStart(string[] args) { base.OnStart(args); snap(); } protected override void OnStop() { base.OnStop(); unSnap(); } protected override void OnPause() { base.OnPause(); unSnap(); } protected override void OnContinue() { base.OnContinue(); snap(); } protected override void OnShutdown() { base.OnShutdown(); unSnap(); } } }