protected override void OnStop() { // Update the service state to Stopped Pending. ServiceStatus serviceStatus = new ServiceStatus { dwCurrentState = ServiceState.SERVICE_STOP_PENDING, dwWaitHint = 100000 }; Win32API.SetServiceStatus(ServiceHandle, ref serviceStatus); m_service_timer.Stop(); // Update the service state to Stopped. serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED; Win32API.SetServiceStatus(ServiceHandle, ref serviceStatus); eventLog1.WriteEntry("Service stopped."); }
// The Service Control Manager uses the dwWaitHint and dwCheckpoint members of the SERVICE_STATUS structure to determine how much time to wait // for a Windows Service to start or shut down. If your OnStart and OnStop methods run long, your service can request more time by calling // SetServiceStatus again with an incremented dwCheckPoint value. // Run Visual Studio Developer command prompt as Adminstrator - // install: installutil.exe MyNewService.exe // uninstall: installutil.exe /u MyNewService.exe protected override void OnStart(string[] args) { // Update the service state to Start Pending. ServiceStatus serviceStatus = new ServiceStatus { dwCurrentState = ServiceState.SERVICE_START_PENDING, dwWaitHint = 100000 }; Win32API.SetServiceStatus(ServiceHandle, ref serviceStatus); m_service_timer = new System.Timers.Timer { Interval = 5 * 60000 }; // 60 seconds m_service_timer.Elapsed += OnTimerElapsed; m_service_timer.Start(); // Update the service state to Running. serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING; Win32API.SetServiceStatus(ServiceHandle, ref serviceStatus); eventLog1.WriteEntry("Service started."); }
public static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus);