-
Nov 22nd, 2013, 09:29 AM
#1
Thread Starter
Frenzied Member
System.Timers.Timer Elapsed event locking?
Trying to make it so that the Timer event doesn't process if another one is still in process, this doesn't seem to be working:
Code:
Public Partial Class srvcCRISReporting
Inherits ServiceBase
Private thisLock As New [Object]()
Private Sub tmrReports_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs)
' Don't fire tick event while another tick event is running
SyncLock thisLock
' Event Code here
End SyncLock
End Sub
End Class
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Nov 22nd, 2013, 09:58 AM
#2
Re: System.Timers.Timer Elapsed event locking?
Have you tried removing the timers event handler when another one is running and then restoring it when finished?
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Nov 22nd, 2013, 11:29 AM
#3
Re: System.Timers.Timer Elapsed event locking?
Do you wan this blocked for all instances of the class, or just the current instance?
-
Nov 22nd, 2013, 11:52 AM
#4
Re: System.Timers.Timer Elapsed event locking?
At first, I was thinking that you might be able to solve the problem by simply locking on a global object, as this could be a scoping issue (though that doesn't seem right). Still, ANY solution that involves locking may prove to be a bad one. Suppose one tick got through and started a process which, for whatever reason, took 5 ticks to complete. At the end of that processing, it would release the lock...and five other threads would be contending for the lock. If the next process took two ticks to complete, you'd then have six threads contending for the lock.
If the processes are sufficiently lengthy that there is a fair chance that they will take long enough for another tick to occur, what mechanism do you have to prevent a massive backlog of threads piling up?
I would suggest that the best solution to this probably has nothing to do with locking. I think that NO tick should wait. You should have a mechanism such that, when the tick occurs, it can decide whether or not it is free to process, and if it is not, it doesn't wait, it just exits. In other words, something like this:
Code:
If Not Busy Then
'Do something.
End If
So that the tick event completes without doing anything if it is not free to proceed. Of course, doing that in a thread safe fashion could be a bit tricky. One option would be a wait with a timeout, another would be an Interlocked.Increment mechanism. What you probably don't want is the tick event handler waiting on a lock under any circumstance, unless you can be sure that the number of waiting threads won't keep building forever.
My usual boring signature: Nothing
-
Nov 22nd, 2013, 12:13 PM
#5
Re: System.Timers.Timer Elapsed event locking?
Or have the timer not constantly tick. Set the AutoReset property to false, and then the Elapsed event is only raised once. The final thing that event handler does is call Start on the timer again, to raise another Elapsed event after the Interval passes.
-
Nov 22nd, 2013, 12:29 PM
#6
Re: System.Timers.Timer Elapsed event locking?
Either what EG suggested or just, you know, stop the timer when the event is called (and then start it once finished). I'm pretty sure, though, that they do the same thing internally.
-
Nov 22nd, 2013, 03:49 PM
#7
Thread Starter
Frenzied Member
Re: System.Timers.Timer Elapsed event locking?
Thanks for the help guys. Think I am going to deal with it database side. I'm running reports and marking them finished, but the long runs were kicking off again before they finished. Going to use a flag to show they are in process.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|