views:

361

answers:

3

Hi,

I want to call a Business layer method from a Windows service (done using C# and .NET) after every 10 seconds. However, i dont want to use the Timer_Elapsed event since it starts up another thread/process if the first thread/process is still running. I just need a single threaded approach, since multiple calls to the same Business method creates unwanted complications.

So i added a do--while loop in the on_start. I know this is not the correct way since it spawns this process which becomes an orphan if the service is shut down.

How can i approach this problem ?

Regards, Chak

+3  A: 

Use a timer, but as soon as you enter the Timer handler method, disable the timer so that no more events are raised. Just before exiting the handler, re-enable the timer.

Mitch Wheat
Thanks. But if i do this, it will call the Business method once and then never run again ? The Business method in turn reads or writes to a serial port and so it should return after one read / write / timeout and then repeat it after some time.
Chakra
it @Chakravarthy: provided you re-enable timer, before leaving the handler, it will continue to run.
Mitch Wheat
Since the handler method is invoked on a separate thread, it could theoretically be called multiple times before the timer is disabled.
Anders Fjeldstad
+2  A: 

Check out this discussion, and in particular the answer by jsw. It suggests a synchronization mechanism to prevent multiple simultaneous calls to the business logic. Just disabling the timer in the Elapsed handler method isn't guaranteed to prevent parallel calls since the handler is invoked on a separate thread. Use a lock as jsw suggests, and stop the timer within the synchronized code block.

Alternatively, you could use a Timer and set the AutoReset property to false. That way, the Elapsed event is raised only once and you can reset the timer manually towards the end of the handler method.

Anders Fjeldstad
+1. Good points.
Mitch Wheat
+1  A: 

There's another way to get timed execution, the WaitHandle.WaitOne() method provides a timeout argument. That works very nicely in a service as it lets you implement the need to stop the service and periodic execution in a single method call. The template looks like this:

Thread mWorker;
AutoResetEvent mStop = new AutoResetEvent(false);

protected override void OnStart(string[] args) {
  // Start the worker thread
  mWorker = new Thread(DoWork);
  mWorker.Start();
}
protected override void OnStop() {
  // Signal worker to stop and wait until it does
  mStop.Set();
  mWorker.Join();
}
private void DoWork(object arg) {
  // Worker thread loop
  for (; ; ) {
     // Run this code once every 10 seconds or stop right away if the service is stopped
    if (mStop.WaitOne(10000)) return;
    // Do work...
  }
}
Hans Passant
Has anyone got a working example of this in action?
Junto