Windows service stops automatically

Either you are not starting any threads on the OnStart method to do work, or there is an exception raised within your OnStart method.

If an exception is thrown, it will appear in the Windows Event log. The Windows Event log is a good place to start in any case.

Generally an OnStart method looks like this:

Thread _thread;

protected override void OnStart(string[] args)
{
    // Comment in to debug
    // Debugger.Break()

    // Do initial setup and initialization
    Setup();

    // Kick off a thread to do work
    _thread = new Thread(new MyClass().MyMethod)
    _thread.Start();

    // Exit this method to indicate the service has started
}

Leave a Comment