3

I have a windows service that runs and starts up fine, however, it runs a background thread (multiple of them) that do some serious computation.

After a bit of digging I have found that it is due to the timeout that all windows services have. Currently, it reads from a table in a database, loads it into an object, then does some analysis on said object. This is all done in the OnStart method which then calls other methods. Is there some trick to keep the service running or any way to stop timeouts without going into the registry? Thanks

Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
Badmiral
  • 1,549
  • 3
  • 35
  • 74

2 Answers2

5

The OnStart function is not the place for logic that takes a long time to execute. Basically you need to create a class for your logic outside of the OnStart function. You'll need to publicly declare an entry function - i.e. the one that gets the data and starts working on it.

e.g.

class ProcessingClass()
{
    public void ThreadStartProc()
    {
         GetData();
         StartProcessing();
    }
 }

In your onStart method, create a new Thread and set the ThreadStart to your ThreadStartProc function. e.g.

Thread ProcessingThread;
ProcessingClass procClass = new ProcessingClass();

protected override void OnStart(string[] args)
{
    ProcessingThread = new Thread(new ThreadStart(procClass.ThreadStartProc));
    ProcessingThread.Start();
}

protected override void OnStop()
{
     if (ProcessingThread != null)
     {
          ProcessingThread.Abort();
          ProcessingThread.Join();
          ProcessingThread = null;
     }
}

In your processing class you'll need to handle the ThreadAbortException that will get thrown when the service is stopped.

dhh
  • 4,289
  • 8
  • 42
  • 59
Swomble
  • 874
  • 8
  • 17
  • I don't fully understand the logic of the OnStop method. If you are processing a thread, then shut it down and make it not process by setting it to null? Not sure I get that. Thanks! – Badmiral Oct 15 '12 at 16:16
  • 1
    The .Abort throws the ThreadAbortException in the ProcessingClass in the example above, allowing you to exit gracefully, tidy up resources etc. But it won't wait for the thread to exit. The Join, waits for the thread to finish tidying up and exits. Finally setting the ProcessingThread to null is just something I tend to do - helps force the .NET garbage collector to do a clean up. – Swomble Oct 15 '12 at 16:25
4
  1. Move your logic out of the OnStart method.
  2. Use the Service.RequestAdditionalTime() method.

See this SO answer: windows service startup timeout

Community
  • 1
  • 1
Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
  • as far as I know there is just OnStart and OnStop, are there any other modes for a windows service to be in in order to transfer the logic to one of those? Thanks – Badmiral Oct 15 '12 at 15:48
  • Why is the db object load taking too long? Can it be refactored? If not, spin that off into a separate thread and then start your large computation once that finishes. – Ralph Willgoss Oct 15 '12 at 15:54