none
Windows Service will not start automatically on reboot

    Question

  • I have a service that I'm working on that's being rather stubborn about wanting to start up on reboot. I should mention out of the gate that the service works, starts up in maybe a second or two normally and even has the ability to start right after it finishes installing without so much as a blink.

    However, I restart the computer and it tempermentally doesn't start itself up. And when I say that I mean it starts up maybe 1% of the time. Here's my OnStart for reference:

    protected override void OnStart(string[] args) { base.RequestAdditionalTime(100000000); MainThread = new Thread(Kickoff); MainThread.Start(); }

    Now the request for additional time doesn't seem to help since the first error is a timeout error for the standard 30 seconds (even when the computer has been on for less than 20, it's a VM). My understanding of the above is that putting stuff in the constructor of the service base is not a good idea, so it's empty apart from InitializeComponent(). That InitializeComponent only says:

    private void InitializeComponent() { components = new System.ComponentModel.Container(); this.ServiceName = "PsykerService"; }

    The kickoff is the major buisness logic. However, what bewilders me is that it doesn't pass the second line at all since the first line is to write something to a text file.

    EventLogging.SetDefaultLog();
    EventLogging.WriteLog("Hope is the first step on the road to dissapointment.");

    So I decided to fumble a bit with dependencies. The service requires the internet, so I latched their startup upon DHCP and DNS services and set delayed startup to true.

    this.Installer.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
    this.Installer.ServicesDependedOn = new string[] { "DNS Client", "DHCP Client" };
    this.Installer.DelayedAutoStart = true;

    But none of this seems to be doing the trick. Is there something I'm missing that could delay the startup of the service or make it more reliable?
    Thursday, January 10, 2013 5:33 PM

All replies

  • Remove your call to set the additional time.  That is only needed if the starting of your service is going to take longer than the allowed time.  When the SCM sees that it doesn't bother checking on the service status again until the time elapses.  In your case you're immediately starting a worker thread (and I assume then returning from OnStart) so your code should be fine.  Note that the SCM considers a service started when OnStart returns.  That doesn't mean your service necessarily is ready to handle requests.  It's up to you to decide what to do between the time your service starts and you're actually ready to handle requests.  In general the only case you would need to worry about is if your service received a stop request right after a start request.

    If you need network access then you should set a dependency on either RPC (if you're using COM) or HTTP (if you require IIS or equivalent).  Network Connections could also be used.

    Also note that setting DelayedAutoStart will cause your service to start some time after the system settles.  In general you shouldn't use delay start on services that need to start at startup.  Also note that Automatic services are started after Windows comes online and potentially before or after a user logs into the system. 

    Michael Taylor - 1/10/2013
    http://msmvps.com/blogs/p3net

    Thursday, January 10, 2013 6:28 PM
    Moderator
  • The point of the asking for more time is to compensate since it keeps timing out. It was one of the things I did to at least try getting it working. Is it doing the trick? I don't know.

    My needed access is two-fold. I need netpipes for inter-process communication (yeah, that again, but I got it working this time) and the service needs to download files from the internet. However, my machine doesn't have an HTTP service. Because of our mixed enviroment, I cannot assume anybody else has it either, so I have to use something more common, like the services I listed.

    As for starting the moment the computer starts up, it isn't that required. If it kicks on after setling, it would be just as fine.

    Thursday, January 10, 2013 6:40 PM
  • Is SetDefaultLog doing anything that might interfere with startup?  Like is logging set to a network location?

    Does your worker thread have an exception handler?  Is it trapping anything that might indicate a failure?


    This signature unintentionally left blank.

    Thursday, January 10, 2013 6:57 PM
  • Set default log nabs a registry key, produces an output stream and sets up the windows logging info. It logs locally.

    Yes. No.

    Thursday, January 10, 2013 7:04 PM
  • Hi,

    Does it start only the first time ? Have you double checked it couldn't be some kind of service naming issue ? For example the service starts but its name is not "PsykerService" or something like that ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, January 10, 2013 7:26 PM
  • I can turn it on manually any time with no error.
    Thursday, January 10, 2013 7:42 PM
  • If you added the request for additional time and all your OnStart does is call Thread.Start then something is amiss.  Is your service's ctor doing anything?  Do you have fields that are initialized to types that have heavy constructors?

    Michael Taylor - 1/10/2013
    http://msmvps.com/blogs/p3net

    Thursday, January 10, 2013 8:42 PM
    Moderator
  • The most initialized a field I have is an Object (as in the class Object).

    But I think something was having compiling issues or something, because I can get it to work now. I need to narrow this down but I feel silly.

    Thursday, January 10, 2013 8:58 PM
  • The most initialized a field I have is an Object (as in the class Object).

    But I think something was having compiling issues or something, because I can get it to work now. I need to narrow this down but I feel silly.

    Did you get the answer? please mark it/them as answer(s).

    From the last reply, I think you didn't want to continue it, did you? I do not know Windows Service much, but I think C# forum maybe not the right forum for this question.


    Monday, January 14, 2013 8:30 AM
  • The answer was it was crashing because I put most of the logic in the constructor, not the OnStart command.
    Monday, February 18, 2013 6:47 PM