Search code examples
c#windows-services

Error while stopping windows service


I am working on Windows Service. It works fine. When i am trying to stop the service from services.msc, it throws the following error:

Windows could not stop the xxx service on Local Computer. The service did not return an error. This could be an internal Windows error or an internal service error. If the problem persists, contact your system administrator.

If I try to stop it again, it takes lots of time and then throws the error as below:

Windows could not stop the xxx service on Local Computer. Error 1061: The service cannot accept control messages at this time.

At this point, the service has stopped. But if I try to reinstall the service, it throws another error:

Windows could not stop the xxx service on Local Computer.
Error 1001: The specified service is marked for deletion.

After closing services.msc, it lets me reinstall the service and again things start working fine.

In OnStop(), I am doing some lengthy operations and it takes time to complete.

Any idea how I can make the whole thing go smoothly?

--Edit--

This is what my OnStop method looks like:

protected override void OnStop()
{
    base.OnStop();
    //Some lengthy operation      
}

Solution

  • The windows service have a default timeout in onstart and onstop events. Normally if you are doing time consuming operations in any of these events start a new thread and let the operation to perform in background.

    Usually the OnStart as well as OnStop events within the windows service are used to initiate a process and the time consuming process will carryout it's execution within a child thread.

    Hope this will solve your issue..