Reentrant Timer in Windows Service?

If you want just skip method invocation while previous method didn't finish just use Monitor. TryEnter(lockObject) before calling your method.

If you want just skip method invocation while previous method didn't finish just use Monitor. TryEnter(lockObject) before calling your method. EDIT: Here's an example - public class OneCallAtATimeClass { private object syncObject; public TimerExample() { syncObject = new object(); } public void CalledFromTimer() { if (Monitor.

TryEnter(syncObject);) { try { InternalImplementation(); } finally { Monitor. Exit(syncObject); } } } private void InternalImplementation() { //Do some logic here } }.

Exactly my thought. You might add an example that shows calling TryEnter at the beginning of the method, and the correct try...finally that calls Monitor. Exit on completion.

– Jim Mischel Dec 10 at 21:06 @JimMischel - sure, added an example – Maxim Dec 11 at 19:47.

I would recommend using System.Threading. Timer for this functionality. You can disable the timer when it executes, process your data, then re-enable the timer.

EDIT: I think it makes more sense to use System.Threading. Timer because there isn't really a reason you need to drop the timer on a design surface, which is pretty much the only reason to use System.Timers.Timer. I really wish MS would remove it anyways, it's wrapping System.Threading.

Timer which isn't all that difficult to use in the first place. Yes, you do risk a problem with re-entrancy which is why I specified to change the timeout toTimeout.Infinite. You won't have this re-entrancy problem if you construct the timer with Timeout.Infinite.

Public class MyClass { private System.Threading. Timer _MyTimer; public MyClass() { _MyTimer = new Timer(OnElapsed, null, 0, Timeout. Infinite); } public void OnElapsed(object state) { _MyTimer.

Change(Timeout. Infinite, Timeout. Infinite); Console.

WriteLine("I'm working"); _MyTimer. Change(1000, Timeout. Infinite); } }.

But you can disable System.Timers. Timer, too. Timer.

Enabled = false. – Jim Mischel Dec 10 at 21:02 plus, I read that timerevents can be evoked, even after the timer has been stopped? Thats basically the reason I use CompareExchange link – Timo Jak Dec 10 at 22:26 @JimMischel: Clarified my answer a bit.

– Bryan Crosby Dec 11 at 17:23 @Bryan: I, too, discourage the use of System.Timers. Timer, because it swallows exceptions, which hides bugs. That said, the wrapping is useful for two reasons: 1) it uses an event model rather than a callback.

The event model is more familiar to . NET programmers in general. Second, if you're using it in a Windows Forms application, setting the SynchronizingObject to the form causes the event handler to be called on the GUI thread, relieving you of having to use InvokeRequired and Invoke in order to access controls.

– Jim Mischel Dec 12 at 4:37 @Timo: Yes, it's possible that a timer callback (or event) can be invoked after the timer has been stopped. But that can only happen if the next timer tick occurs before the previous tick's callback stops the timer. You would need a very fast timer or a very slow response for this to happen.

– Jim Mischel Dec 12 at 4:44.

You correctly use CompareExchange to test and set the m_synchPoint field when doing the initial check. You incorrectly use direct assignment to reset the value to 0 at the end of the method. You should use Interlocked.

Exchange instead to reset the value to 0. As a side note, you should also change m_synchPoint to an instance field -- it should not be static.

Id say "useful" but I cant xD – Timo Jak Dec 10 at 23:21.

You can try this: When the timer fires, disable the timer. When the task is complete, re-enable the timer...possibly in the Finally clause.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions