31

I made a Window service and let it work automatically and under localsystem account, when the service starts it fires this message for me and then stops

The [service name] service on local computer started and then stopped. Some Services stop automatically if they are not in use by another services or programs.

What's the problem and what's the solution?

bluish
  • 23,093
  • 23
  • 110
  • 171
netseng
  • 2,126
  • 6
  • 22
  • 28

8 Answers8

40

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
}
Robert Wagner
  • 16,500
  • 8
  • 53
  • 71
  • 1
    +1 helped a lot. I want to note (at least for Windows 7), the [Event Viewer](http://windows.microsoft.com/en-us/windows/open-event-viewer#1TC=windows-7) contains the Windows Logs (then check under Application). – But I'm Not A Wrapper Class Jan 22 '15 at 19:25
6

This particular error message means what it says - that your service has started but then quite soon it exited for some reason. The good news is that your service is actually doing something, so you have the executable configured and running as a service properly.

Once started, for some reason it is quitting. You need to find out why this is. Add some debugging to tell you its up and running and known exit cases. If that doesn't reveal the problem then add some debugging to let you know it's still running and work backwards from when that stops.

bluish
  • 23,093
  • 23
  • 110
  • 171
Mat
  • 67,636
  • 33
  • 83
  • 106
3

I had a similar problem that occurred because my Event Logs were full and the service was unable to write to them. As such, it was impossible to debug by looking for messages in the Event Viewer. I put a try/catch and dumped the exception out to a file. I had to change the settings on my logs to fill as needed instead of every 7 days and this allowed the services to start.

Of course, the root of the problem for me is that I have a nVidia driver issue that is flooding my event logs and now I'm probably beating on the disk, but that's another issue.

Ken Goodridge
  • 3,716
  • 1
  • 17
  • 22
3

Are you tracing out any debug information? Most likely an exception is being thrown during your initialization. I would trace out all your exceptions and use Debugview to view them.

Mike_G
  • 14,627
  • 11
  • 63
  • 93
1

I had the same issue starting JBoss, then I changed the JAVA_HOME variable, it worked for me. It was the JBoss version that doesn't support the 1.6, it supports 1.5.

bluish
  • 23,093
  • 23
  • 110
  • 171
Buminda
  • 11
  • 1
1

Maybe you need to run the service as Local System Account. See this post by Srinivas Ganaparthi.

bluish
  • 23,093
  • 23
  • 110
  • 171
suresh
  • 21
  • 1
0

I had similar problem and it turned out in my case that the program simply crashed in OnStart method. It tried to read some file that it couldn't find but I suppose that any other program crash would give the same result. In case of Windows forms application you would get some error message but here it was just "your service started and stopped"

If you ever need, like me to read some files from the directory where Windows Service .exe is located, check this topic: Getting full path for Windows Service

Community
  • 1
  • 1
RRM
  • 3,065
  • 6
  • 19
  • 36
0

In my case, a method in my service, was being called recursively (as no terminate condition being true) and after specific time my service was being stopped.

umesh shukla
  • 106
  • 2
  • 10