Some Services stop automatically if they are not in use by other services

104,794

Solution 1

This is generally the result of one of two things - either (a) your OnStart() method is throwing an exception or (b) the OnStart() method is not kicking off a thread to do work.

If the problem is (a), then the obvious solution is to debug the service to identify what is going wrong. At a minimum, put a try-catch block around the contents of the OnStart() method and log an error to the system event log when an exception occurs. Then you can see the details in the Windows Event Viewer.

If the problem is (b), then you need to create a thread that actually does something. The thread needs to be a foreground thread (as opposed to a background thread) to prevent the service from shutting down. A typical OnStart() method looks like this:

private System.Threading.Thread _thread;

protected override void OnStart(string[] args)
{
    try
    {
        // Uncomment this line to debug...
        //System.Diagnostics.Debugger.Break();

        // Create the thread object that will do the service's work.
        _thread = new System.Threading.Thread(DoWork);

        // Start the thread.
        _thread.Start();

        // Log an event to indicate successful start.
        EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);
    }
    catch (Exception ex)
    {
        // Log the exception.
        EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
    }
}

private void DoWork()
{
    // Do the service work here...
}

Solution 2

I got this error and it was because the hard drive had filled up. It could be anything that keeps the service from running.

Solution 3

I could fix this by adjusting the service properties.

I noticed that all of the other working services were using "Local System" as "Log On As", but the service having issue was using "Local Service". You can fix this by going to the list of services, right click on desired service, click properties on context menu and then change "Log On As" to "Local system account" if it is not the same as shown in below image.

enter image description here

Share:
104,794
paras_doshi
Author by

paras_doshi

Blog: http://www.parasdoshi.com MSDN profile: http://social.msdn.microsoft.com/profile/paras%20doshi/?type=forum (Microsoft Community Contributor 2011 Award Recipient)

Updated on June 08, 2021

Comments

  • paras_doshi
    paras_doshi almost 3 years

    Error "SOME SERVICES STOP AUTOMATICALLY IF THEY ARE NOT IN USE BY OTHER SERVICES" while trying to start a windows service.

    I have a service that does not use the windows service config file and uses static properties - it works fine

    Now, i make use of app.config file and rebuild my setup project + the service project. Now i install the service and then try to start the service - i get the following error:

    SOME SERVICES STOP AUTOMATICALLY IF THEY ARE NOT IN USE BY OTHER SERVICES

    Service logs on as local system.

    Any input is welcome please! Thanks.

  • Manoj Savalia
    Manoj Savalia over 10 years
    I have also getting same error and got solution from this answer... Thanks.
  • asraful009
    asraful009 about 9 years
    yes i find error on services into Windows Event log.
  • Asif Mehmood
    Asif Mehmood almost 9 years
    Update the answer as well brother. Correct the spelling of EventLog.... Helpful answer, thanks @MattDavis