Windows service will not start (Error 1053)

Although this thread is rather old, here my additional answer, as it might help other save time where I put some hours into:

Situation

  • Windows service written in C# that monitors a directory and posts parsed message from file to MQTT topic runs fine on Win10 and also during short tests on WinXP at home

  • In production environment, service runs on Win10 fine, on WinXP crashes after some hours and cannot be restarted (Could not start the filemqttservice service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion)

Solution

Clear event log history! (Doh!) My service was too chatty to fill the event log buffer in some hours. After clearing the event log, the service started again. Of course, I removed all the lines producing messages from the service


You are missing ServiceBase.Run call:

ServiceBase[] servicesToRun = new ServiceBase[]
                                { 
                                    new MyService() 
                                };
ServiceBase.Run(servicesToRun);

It might also be a good idea to subscribe to unhandled exceptions notification:

static void Main() {
    ...
    AppDomain.CurrentDomain.UnhandledException 
                                      += CurrentDomain_UnhandledException;
    ...
}

private static void CurrentDomain_UnhandledException(
                                                 Object sender, 
                                                 UnhandledExceptionEventArgs e) {

    if (e != null && e.ExceptionObject != null) {
        // log exception:
    }
}

And add following try/catch to OnStart because .NET/SCM swallows exceptions:

protected override void OnStart(String[] args) {
    try {

    } catch(Exception e) {
        // log exception:
        throw;
    }
}