简体   繁体   中英

When installing Windows service and then trying to run, I get an Error 1053. Saying my service is taking too long to start

Error 1053: windows service not start timely fashion

I have set up my service exactly how it was shown in countless online tutorials, but my OnStart method still doesn't seem to be getting called at all. This process is supposed to run and then loop infinitely and constantly check a server for commands to run. Here's my Main:

static void Main(string[] args)
{
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new AgentService(args)
        };
        ServiceBase.Run(ServicesToRun);
}

Here's what my service constructor and OnStart look like:

public AgentService(string[] args)
{
        InitializeComponent();
        this.Args = new ServiceArguments();
        this.ValidArgs = this.Args.SetArgs(args);
        this.AgentCycle = Int32.Parse(Args.Cycle);
}

protected override void OnStart(string[] args)
{
        ServiceStatus serviceStatus = new ServiceStatus
        {
            dwCurrentState = ServiceState.SERVICE_START_PENDING,
            dwWaitHint = 100
        };
        this.CimCommands = new Dictionary<string, CimCommand>();
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);
        serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);

        if (this.ValidArgs) 
           RunAgent();
}

I am handing in args via the command line, but if none are given, I am using environment variables. All are present on my machine so I can't understand why my service is not starting in a "timely manner". I only have 2 extra dependencies. One is Newtonsoft.Json , the other is RestSharp .

I've also tried writing it as a console app using TopShelf, but I had the same error that way. The logic is all sound, I've already tested my methods. Any ideas?

EDIT : okay, here's the best way to explain the goal of this service... The service is supposed to call an api/server every 5 seconds and then receive a json that tells it what command to run on the windows machine. once it finishes all those commands, it asks the server again if there is any work to complete and it starts all over. The custom types are simply json_objects to use with Newtonsoft and the Service Arguments is just an object with 5 string values, and 1 bool. And then functions for parsing the arguments from either the cmd line, environment variables, or a config file.

Yes, there is most definitely an infinite loop in the function being called from RunAgent.

While RunAgent() does not return, OnStart() does not. When OnStart() does not return, the Service Manager will just kill the process because "it takes to long to start". Do not put infinite loops into the OnStart Function!

For once the message is exactly what it says. It also does exactly waht it is there for (getting you to fix your faulty code). For polling you need something like a Timer that you start in "onStart()". Maybe a seperate Background Task or Thread. Not a custom loop you block OnStart() with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM