Hibernation and Windows Services

I am working on a Windows service that has some problems with Thread.Sleep() , so I decided that I would try to use a timer instead, as this question recommends:

Using Thread.Sleep () in a Windows Service

The thing is not entirely clear to me how this can be implemented. I believe this is so, but I just wanted to make sure:

 '' Inside The Service Object Dim closingGate As System.Threading.AutoResetEvent Protected Overrides Sub OnStart(ByVal args() As String) Dim worker As New Threading.Thread(AddressOf Work) worker.Start() End Sub Protected Sub Work() Dim Program = New MyProgram() closingGate = New System.Threading.AutoResetEvent(False) Program.RunService(closingGate) End Sub Protected Overrides Sub OnStop() closingGate.Set() End Sub '' Inside My Programs Code: Public Sub RunService(closingGate As AutoResetEvent) Dim tmr As New Timer '' ...and so on closingGate.WaitOne() End Sub 

Besides using VB.Net (Kidding, but I would use C # if I could). Am I on the right track here? Is this better than using Thread.Sleep() ?

+7
multithreading sleep timer windows-services
source share
2 answers

Honestly, I have to say that I think you are a little bit off of here.

First of all, the actual code does not make sense; the worker thread is waiting for a signal, but for no reason - it is not actually in any cycle, nor in expectation of a resource. Secondly, if you needed to execute some (possibly omitted) cleaning code from a worker after receiving a shutdown signal, your service may not give the worker thread enough time to clear it.

But, more fundamentally, all you have done is move the problem to a separate thread. This can cause the service to respond to the service controller, but does not eliminate the design problem - and this adds a lot of unnecessary complexity using threads and mutexes; ultimately, it will just make your maintenance more difficult to debug if you ever need to.

Let's say you need to execute some kind of code every 5 seconds. The idea is that instead of "waiting" for 5 seconds, you invert the control, and let the timer invoke your work.

This is bad:

 protected override void OnStart(string[] args) { while (true) { RunScheduledTasks(); // This is your "work" Thread.Sleep(5000); } } 

It's good:

 public class MyService : ServiceBase { private Timer workTimer; // System.Threading.Timer protected override void OnStart(string[] args) { workTimer = new Timer(new TimerCallback(DoWork), null, 5000, 5000); base.OnStart(args); } protected override void OnStop() { workTimer.Dispose(); base.OnStop(); } private void DoWork(object state) { RunScheduledTasks(); // Do some work } } 

What is it. This is all you need to do to work at regular intervals. As long as the work itself does not work for several seconds, your service will respond to the service controller. You can even support suspension in this scenario:

 protected override void OnPause() { workTimer.Change(Timeout.Infinite, Timeout.Infinite); base.OnPause(); } protected override void OnContinue() { workTimer.Change(0, 5000); base.OnContinue(); } 

The only time you need to start creating workflows in your service is when the work itself can work very long and you need to cancel in the middle. This is usually associated with a lot of development efforts, so I will not delve into it, not knowing for sure that this is related to your scenario.

It’s better not to start introducing multi-threaded semantics into your service if you do not need it. If you are just trying to plan the work of small units of work, you definitely do not need to.

+19
source share

I recently wrote a service that was supposed to do some work, and then wait n minutes, then a loop.

I also used ManualResetEvent (called _evt) and a call to Set () in OnStop. But I do not need a timer.

In the service thread, I had:

 for (;;) { // do some work if (_evt.WaitOne(_waitInterval)) { // got the signal => time to leave break; } } 

So either timeout => time has occurred for some extra work. Either Set () is called, and the loop ends.

+1
source share

All Articles