Results 1 to 7 of 7

Thread: System.Timers.Timer Elapsed event locking?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    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.

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: System.Timers.Timer Elapsed event locking?

    Do you wan this blocked for all instances of the class, or just the current instance?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,990

    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

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  6. #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.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    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
  •  



Click Here to Expand Forum to Full Width