The service could not be started, but does start?

15,846

The OnStart event is used to launch a background thread that will take care of the processing. If the OnStart method finishes without error, the service manager assumes the service started successfully. As such, the OnStart should return as soon as possible.

The OnStop method is then used to stop the background processing. A successful OnStop tells the service manager that the service was closed without error.

Share:
15,846
Floris Devriendt
Author by

Floris Devriendt

Updated on June 15, 2022

Comments

  • Floris Devriendt
    Floris Devriendt almost 2 years

    I've written a small windows service that I want to run daily on my Windows Server 2008. The service is written in C#.

    • The code works great in a normal Windows Form.
    • The service works like it should when I start and stop it from the Services Management window (services.msc).

    But when running it on a commandline with:

    net start Service1
    

    I get the the following:

    The Service1 service is starting........  The Service1 service could not be started.
    
    The service did not report an error.
    
    More help is available by typing NET HELP:SG 3534.
    

    The weird thing is though, the service is still running, in the Services screen I still see it as starting untill it's fully started. When I try to stop the service afterwards I get:

    The service could not be controlled in its present state.
    
    More help is available by typing NET HELPMSG 2189
    

    And then the service is stopped. Is there any way in solving this? I've already managed to debug the service without any problems, the code works. But during debugging the same thing still happens on the command console, while I'm still able to debug further.

    It's like there is some kind of timeout on the onStart() method.. I have no idea.. I'm fairly new to Windows Services (this is my first one). I do write all my code in the onStart() method, maybe that's not the best idea, but I don't know where else to type it.

    If someone could help I'd greatly appreciate it.

  • Floris Devriendt
    Floris Devriendt almost 13 years
    I turned it in to a thread and that actually did the trick. Thank you very much. The only thing I'm wondering now, is there a possibility to stop the service automatically after the thread has finished its job?
  • Floris Devriendt
    Floris Devriendt almost 13 years
    Or do I even need to stop it? I mean, if I create a .bat file that starts and stops my service, the thread will still run, right? Or is this not a good way of working?
  • Johann Blais
    Johann Blais almost 13 years
    Some windows services stop themselves automatically after they finish processing. You can decide to do the same (by calling the Stop method from within the service), but in any case, make sure you stop the background processing gracefully into the OnStop method.
  • Floris Devriendt
    Floris Devriendt almost 13 years
    Thank you for your answers, they will help me a lot. One last thing, I assume myThread.abort() is not a way to stop the background processing gracefully? Will working with a ManualResetEvent do the trick to end a thread correctly?
  • Johann Blais
    Johann Blais almost 13 years
    Thread.Abort should be avoided. You could periodically check from within the thread function if a flag (bool or whatever) has been set, and if it was, then exit the function. You could set this flag from the OnStop method.
  • Karan Raiyani
    Karan Raiyani over 5 years
    You can Stop your service using "Task Manager". You can find your service in "Process" Tab and End that by click "End Process" button. and than fix your code and again Start service.