1

I have a basic Windows service which performs some tasks, which are executed periodically in an endless loop: "ExecuteServiceWorkerMethods()". I am starting the endless loop via a worker thread from the OnStart() method as below:

OnStart(string[] args)
{
       workerThread = new Thread(ExecuteServiceWorkerMethods);
       workerThread.Name = "ServiceWorkerThread";
       workerThread.IsBackground = false;
       workerThread.Start();
}

Now I am wondering what to do with the worker thread in the OnStop() method?

My endless loop looks like this:

private void ExecuteServiceWorkerMethods()
{
      while (!serviceStopped)
      {

    DO WORK....

          while (servicePaused)
          {
              Thread.Sleep(sleepTimeMillisecondsWhileServicePaused);
          }
      Thread.Sleep(sleepTimeMillisecondsWhileServiceNotStopped);
      }
}

Remember this is all very basic. I just want to be able to start and stop my Windows service.

Rune Hansen
  • 954
  • 3
  • 16
  • 36
  • How long are your two sleep time durations for paused and stopped? How long does the DO WORK portion take? Can you show your current OnStop method? – hatchet - done with SOverflow Jul 07 '13 at 13:18
  • ExecuteServiceWorkerMethods() is an endless loop. In the DO WORK various methods are execured periodically. The sleepTime variables are currently 300 ms. Currently I am using workerThread.Abort() in the OnStop() method. – Rune Hansen Jul 07 '13 at 13:56
  • possible duplicate of [How to have a loop in a Windows service without using the Timer](http://stackoverflow.com/questions/2032808/how-to-have-a-loop-in-a-windows-service-without-using-the-timer) – Hans Passant Jul 07 '13 at 15:01
  • I normally use a `ManualResetEvent` object in place of your `serviceStopped` variable. Unless you have something that needs to happen when the thread stops (e.g., close database connection, close file, etc.), calling Abort() is ok for a simple shutdown scenarios since the service is going away anyway. – Matt Davis Jul 07 '13 at 16:54

1 Answers1

0

It appears that your code follows the code found here pretty closely.

You should be able to use something like this:

protected override void OnStop()
{
   // flag to tell the worker process to stop
   serviceStopped = true;

   // give it a little time to finish any pending work
   workerThread.Join(new TimeSpan(0,2,0));
}

The call to .Join will cause the thread to terminate.

Brad Bruce
  • 7,638
  • 3
  • 39
  • 60
  • Hi Brad Bruce, thank you for your input. But in listing 7, of the above link, is written: Thread.CurrentThread.Abort(); As far as I can see this code will be executed when the OnStop() method is executed? Is that Ok? Doesn't it throw an exception? – Rune Hansen Jul 07 '13 at 15:47
  • Wow. I didn't see that. It doesn't serve any purpose. By the time you get to that line, your thread is about to terminate anyway. Just comment out the .Abort line at the end. – Brad Bruce Jul 07 '13 at 16:20
  • So I have implemented workerThread.Join(aLittleTime) in my Windows Service and it seems to start and stop when I want. – Rune Hansen Jul 07 '13 at 17:45