myServiceStatus.currentState = (int)State.SERVICE_START_PENDING;
myServiceStatus.checkPoint = 1;
myServiceStatus.waitHint = 5000;
SetServiceStatus(handle, ref myServiceStatus)
After your worker thread(s) start and are your service is running, call (before OnStart ends):
myServiceStatus.currentState = (int)State.SERVICE_RUNNING;
myServiceStatus.checkPoint = 0;
myServiceStatus.waitHint = 0;
SetServiceStatus(handle, ref myServiceStatus);
For more on writing services in .NET please search Google on: 'SetServiceStatus' and 'ServiceBase'
[DllImport("advapi32.dll")]
private static extern bool SetServiceStatus(IntPtr hServiceStatus, ref SERVICE_STATUS lpServiceStatus);
Declare Function SetServiceStatus Lib "advapi32.dll" (
ByVal hServiceStatus As IntPtr,
ByRef lpServiceStatus As SERVICE_STATUS)
As Integer
public enum State
{
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 SERVICE_STATUS {
public long serviceType;
public State currentState;
public long controlsAccepted;
public long win32ExitCode;
public long serviceSpecificExitCode;
public long checkPoint;
public long waitHint;
};
1) The Microsoft MSDN Site is incorrect in their example for C#! They do not call the lpServiceStatus argument using the ref modifier.
2) To have this work on all platforms (64-bit and 32-bit) you must use the IntPtr. Never use an int/Integer as it does not adapt.