Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
down vote
favorite


I am using a system.timer in a Windows Service to run a process that usually exceeds the timer's interval. I am trying to keep the timer from firing the same code more than once, a known issue with system.timers.

What I want: The timer runs my code, but the timer "pauses" to wait until the code is completed before resuming ticks.

I have two problems:

1. The way system.timers work is that the timer will create a race condition on you by launching new redundant threads of the same code and pile them up on you if the has not completed by the time the timer's interval has elapsed.

2. I would start/stop the timer to keep this from happening, but with a System.Timers.Timer, once you stop the timer for the processing to complete, it never comes back - I have never been able to restart a timer once it has been stopped, it has been destroyed and likely collected. Enabling/disabling is the same exact thing as start/stop with same results.

How on earth do you keep a system.timer from launching new redundant threads of the same code if the process has not completed by the time the timer's interval has lapsed? Obviously, starting/stopping (enabling/disabling) the timer is NOT a solution, as it doesn't work.

Help!
Posted
Updated 21-Feb-17 15:10pm
Comments
Sergey Alexandrovich Kryukov 28-Aug-15 20:32pm    
I don't know such class System.Timer? What timer do you really use? Why using timer at all?
—SA
PIEBALDconsult 28-Aug-15 21:53pm    
You can disable it at the beginning of the method and re-enable it at the end.

something to try.
its not exactly pausing the timer, just skipping over the code until it gets done executing.

VB
'set a global variable
Public PassOver as Boolean = False


then in the code that the timer fires use the following:

VB
if PassOver = false then
   PassOver = true
   'place code to fire here
   PassOver = False
end if
 
Share this answer
 
You can't stop a system timer, never.
all you can do is disable the timer or make your code safe against running more than once.

You need a permanent variable to store the program status.

VB
If Busy Then Return
End If
Busy = True

' do your stuff here

Busy= False
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Aug-15 20:34pm    
Which timer class are you talking about?
—SA
Patrice T 28-Aug-15 21:27pm    
For me, a timer is linked to RTC.
With Windows, the word timer is a wrap for something pretty sophisticated.
You can try using the System.Threading class.

The code will look something like this :


VB
Sub Test()
     Dim myThread As System.Threading.Thread = Nothing
     Dim myWebBrowser As WebBrowser = Nothing
     myThread.Start(myWebBrowser)

     ' Suspends the current thread for a specified time
     Threading.Thread.Sleep(10000)
 End Sub
 
Share this answer
 
Comments
Graeme_Grant 21-Feb-17 22:21pm    
this is just wrong!
Neil Tsakatsa 12-Apr-17 21:54pm    
This guy was talking about pausing the timer. The timer control cannot be paused and by the way, the timer acts as some form of a loop, so instead of using the timer, you can put the code into a thread that can be paused/stopped and then reignited again ! It's correct if you want your code to have some breaks/lapses.
Graeme_Grant 12-Apr-17 22:05pm    
You don't apply a kludge fix to an incorrect way of doing things.

You also missed my point totally unlike Ralf. Why answer a 2-year-old tombestoned question??
Neil Tsakatsa 13-Apr-17 4:30am    
There are many ways to kill a cat ! If I think I have something to share I have to share it iregardless of whether it's a 2 or 10 year old question. Someone might be having the same problem right now, this guy is not the first one having this problem and probably not the last. That answer will probably help someone maybe 5 years from now ! That's what open source community is for. If you are not a fan of open source, you should probably try Apple !
Graeme_Grant 13-Apr-17 4:39am    
You may want to read the website's posting T&Cs...

I look forward to your Open Souce community article contributions then.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900