C# Windows service immediately "running" instead of "start-pending"

0

I want to write a Windows service to initialize a hardware-device on Windows Boot. Unfortunately, the initialization needs up to 10 seconds. Since another service is depending on it (and should just start if my service has been finished) the service status should be held onSERVICE_START_PENDINGduring the initialization and change toSERVICE_RUNNINGafter all was done.

Due to the fact that I am capturing data by using asynchronous processes (with events), I can not do the whole processing in theOnStartfunction. Based on the information from the Microsoft website I tried the following procedure:

protected override void OnStart(string[] args)
{
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
    serviceStatus.dwWaitHint = 100000;
    serviceStatus.dwControlsAccepted = 0x00000000; // none
    serviceStatus.dwCheckPoint= 1;    // Initialize checkpoint
    SetServiceStatus(this.ServiceHandle, ref serviceStatus);

    /* Starting the asynchronous processes */
}



void OutputDataReceived_Event(object sender, DataReceivedEventArgs e)
{
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
    serviceStatus.dwControlsAccepted = 0x00000000;
    serviceStatus.dwCheckPoint++;    // Increment checkpoint
    SetServiceStatus(this.ServiceHandle, ref serviceStatus);

    /* Do initialization things */
    /* Call "InitializationDone()" if all was done */
}



void InitializationDone()
{
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
    serviceStatus.dwControlsAccepted = 0x00000001; // SERVICE_ACCEPT_STOP
    SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}

However, the Service Control Manager immediately indicates my service asSERVICE_RUNNINGafter theOnStartmethod has been executed, which cause to start the depending service too early. How can I delay the service statusSERVICE_RUNNING?

I already spent many hours on that topic without success!!

c#
windows
asynchronous
process
windows-services
asked on Stack Overflow Oct 20, 2018 by AustriaBen • edited Oct 20, 2018 by AustriaBen

1 Answer

0

I see two ways around this:

1) Block in OnStart until the startup is actually finished. The Service manager is well developed. It should have some way to deal with services that have a long startup time. It should be able to start some other services in paralell, asuming they do not run in the same SVCHost instance. And even if they run in the same instance, the instance themself have some multitasking.

And when in doubt, you can just write your own Executeable that registers as a serivce. Then no intereference should be possible.

2) Stop using Pending to communicate the status. Usually if you want to talk with a local service, you talk by sending network packages at the LoopbackIP Adress. Or use pipes (wich do somewhat the same thing).

Just modify the Providing Service so it can tell "I am here, but not yet ready to process". Something it will stop saying if it's internal startup is complete.

answered on Stack Overflow Oct 20, 2018 by Christopher

User contributions licensed under CC BY-SA 3.0