简体   繁体   中英

ServiceController.Start() don't reach “Running” status

As the title says, I'm trying to start a service created at run-time, but I always end-up getting the 'timeout' exception, as the service status does not change to Running and hangs on Starting forever.

Here's my StartService() function:

private void StartService()
{
    ServiceController service = new ServiceController(serviceName);
    try
    {                    
         SaveProject();
         if (!CheckServiceExist(serviceName))
             CreateService();
         TimeSpan timeout = TimeSpan.FromMilliseconds(30000);
         service.Start();
         service.WaitForStatus(ServiceControllerStatus.Running, timeout);
     }
     catch (Exception ex)
     {
         Trace.Writeline(ex.ToString());
     }
}

I can confirm that CreateService() is doing what is supposed to do flawlessy.

When I call from Visual Studio (2013) the first time, it occurs as described. BUT, when I run again, it works like a charm! When I run outside Visual Studio, it hangs on Starting and I can't either Start or Stop it through Windows Task Manager nor Windows Services.

I'm using WCF and Windows Service, and the StartService() is called from a button Command, if that helps at all. Thanks in advance.

Please take a look at this MSDN page.

Under Setting Service Status you will find " If a service takes a little while to start up, it might be helpful to report a Start Pending status ". There is a detailed explanation how to tell the windows service manager that your service is still in startup process and avoid a timeout.

// Update the service state to Start Pending.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);

But this will only help if the timeout is really the problem on your service implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM