Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order Of Method Calls in a Windows Service

In what order to the method calls happen in a windows service? We are having a timing issue and I think it is due to the constructor taking too long to complete processing.

This is a similar issue that I think we are having Error 1053 the service did not respond to the start or control request

However, we are wonder in what order and when the methods Main, OnStart, InitializeComponent etc get called and/or when they should be called.

As well, would the OnStart method be the best place to put all of the processing?

**Solution

Made some time to make a test service and tested the responses below and found out the time out is coming from the Main method processing as well as the constructor being called in the Main method causing it to time out. Moving everything to the OnStart as well as just spinning off a thread seemed to work. Details here. http://www.adamthings.com/post/2012/06/28/error-1053-the-service-did-not-respond-to-the-start-or-control-request-in-a-timely-fashion/

like image 414
Adam Avatar asked Jun 28 '12 17:06

Adam


1 Answers

Do not block in the OnStart() method. It sounds like you are doing this.

Create a separate thread in the OnStart() method and get out of there.

I haven't tested this, but something similar to:

protected override void OnStart(string[] args)
{
    var worker = new Thread(DoWork);
    worker.IsBackground = false;
    worker.Start();

    base.OnStart(args);
}

private void DoWork()
{
    while (!_stopRequested)  // (set this flag in the OnStop() method)
    {
        // processing goes here
    }
}

To answer your question about the order of events, just setup Trace statements in each method. I'm not sure how helpful that will be. The message (The service did not respond to the start request ...) you are getting is indicative of blocking (or taking too long) in a method that should execute relatively quickly.

like image 195
jglouie Avatar answered Sep 28 '22 03:09

jglouie