473,232 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

System.Threading.Timer does not tick

I have a .net service that runs a System.Threading.Timer. The delegate
points to the function that the service should execute when the timer
elapses. Problem: The timer is not ticking.

I have never used this timer. I used the documentation from Microsoft
as a guide, but I cannot get this timer to work.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim myTimer As New TimerState()

' Create the delegate that invokes methods for the timer.
Dim timerDelegate As New TimerCallback(AddressOf
checkDatabases)

' Create a timer that waits 0 seconds, then invokes every 60
seconds.
Dim timer As New Timer(timerDelegate, myTimer, 10000, 60000)

log.write("Service started ")

End Sub

The log.write("Service....") executes properly. Why is the timer not
working ?

Dec 14 '06 #1
8 6802
>
' Create a timer that waits 0 seconds, then invokes every 60
seconds.
Dim timer As New Timer(timerDelegate, myTimer, 10000, 60000)
I've never created a timer this way. I usally start it explicitly
myTimer.start()

Dec 14 '06 #2
And as soon as the log.write("Service started ") the OnStart event handler
ends and your timer ceases to exist!!!!!!!!!

Because ... you have declared as local to the event handler!

Move it (and your state object) to class scope or higher and you should be
in business.

Private m_timer As Timer = Nothing
Private m_myTimer As New TimerState

Protected Overrides Sub OnStart(ByVal args() As String)

m_timer = New Timer(AddressOf checkDatabases, m_myTimer, 10000, 60000)

log.write("Service started ")

End Sub

Note also that, in VB.NET, you don't have to instantiate a delegate for the
TimerCallback. You can use AddressOf <methoddirectly.

Now for an anomily. Your comment says 'Create a timer that waits 0 seconds,
then invokes every 60 seconds.' but the duetime value specified in the
constructor is 10000 which indicates 10 seconds. As writ, the Timer will
'fire' after 10 seconds and then every 60 seconds after that.
"KnighT" <br**********@yahoo.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>I have a .net service that runs a System.Threading.Timer. The delegate
points to the function that the service should execute when the timer
elapses. Problem: The timer is not ticking.

I have never used this timer. I used the documentation from Microsoft
as a guide, but I cannot get this timer to work.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim myTimer As New TimerState()

' Create the delegate that invokes methods for the timer.
Dim timerDelegate As New TimerCallback(AddressOf
checkDatabases)

' Create a timer that waits 0 seconds, then invokes every 60
seconds.
Dim timer As New Timer(timerDelegate, myTimer, 10000, 60000)

log.write("Service started ")

End Sub

The log.write("Service....") executes properly. Why is the timer not
working ?

Dec 14 '06 #3

Stephany Young wrote:
And as soon as the log.write("Service started ") the OnStart event handler
ends and your timer ceases to exist!!!!!!!!!

Because ... you have declared as local to the event handler!

Move it (and your state object) to class scope or higher and you should be
in business.

Private m_timer As Timer = Nothing
Private m_myTimer As New TimerState

Protected Overrides Sub OnStart(ByVal args() As String)

m_timer = New Timer(AddressOf checkDatabases, m_myTimer, 10000, 60000)

log.write("Service started ")

End Sub

Note also that, in VB.NET, you don't have to instantiate a delegate for the
TimerCallback. You can use AddressOf <methoddirectly.

Now for an anomily. Your comment says 'Create a timer that waits 0 seconds,
then invokes every 60 seconds.' but the duetime value specified in the
constructor is 10000 which indicates 10 seconds. As writ, the Timer will
'fire' after 10 seconds and then every 60 seconds after that.
"KnighT" <br**********@yahoo.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
I have a .net service that runs a System.Threading.Timer. The delegate
points to the function that the service should execute when the timer
elapses. Problem: The timer is not ticking.

I have never used this timer. I used the documentation from Microsoft
as a guide, but I cannot get this timer to work.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim myTimer As New TimerState()

' Create the delegate that invokes methods for the timer.
Dim timerDelegate As New TimerCallback(AddressOf
checkDatabases)

' Create a timer that waits 0 seconds, then invokes every 60
seconds.
Dim timer As New Timer(timerDelegate, myTimer, 10000, 60000)

log.write("Service started ")

End Sub

The log.write("Service....") executes properly. Why is the timer not
working ?
Thank you Stephany, I will give that a try. That makes sense and
hopefully is the solution I am looking for. I was just playing around
with the due time and did not change the comment.

Dec 14 '06 #4

KnighT wrote:
Stephany Young wrote:
And as soon as the log.write("Service started ") the OnStart event handler
ends and your timer ceases to exist!!!!!!!!!

Because ... you have declared as local to the event handler!

Move it (and your state object) to class scope or higher and you should be
in business.

Private m_timer As Timer = Nothing
Private m_myTimer As New TimerState

Protected Overrides Sub OnStart(ByVal args() As String)

m_timer = New Timer(AddressOf checkDatabases, m_myTimer, 10000, 60000)

log.write("Service started ")

End Sub

Note also that, in VB.NET, you don't have to instantiate a delegate for the
TimerCallback. You can use AddressOf <methoddirectly.

Now for an anomily. Your comment says 'Create a timer that waits 0 seconds,
then invokes every 60 seconds.' but the duetime value specified in the
constructor is 10000 which indicates 10 seconds. As writ, the Timer will
'fire' after 10 seconds and then every 60 seconds after that.
"KnighT" <br**********@yahoo.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>I have a .net service that runs a System.Threading.Timer. The delegate
points to the function that the service should execute when the timer
elapses. Problem: The timer is not ticking.
>
I have never used this timer. I used the documentation from Microsoft
as a guide, but I cannot get this timer to work.
>
Protected Overrides Sub OnStart(ByVal args() As String)
Dim myTimer As New TimerState()
>
' Create the delegate that invokes methods for the timer.
Dim timerDelegate As New TimerCallback(AddressOf
checkDatabases)
>
' Create a timer that waits 0 seconds, then invokes every 60
seconds.
Dim timer As New Timer(timerDelegate, myTimer, 10000, 60000)
>
log.write("Service started ")
>
End Sub
>
The log.write("Service....") executes properly. Why is the timer not
working ?
>

Thank you Stephany, I will give that a try. That makes sense and
hopefully is the solution I am looking for. I was just playing around
with the due time and did not change the comment.
This timer never works. I created a simple windows form to test the
timer and that ticks. The timer in my service never does what it is
supposed to do.

Even if the scope from my first post was incorrect, then it should
still call the delegate and execute the method checkDatabases AT LEAST
ONCE during the onStart(). But it never calls the delegate. WHY ?????
Source code is supposed to do it's job.

Dec 14 '06 #5

KnighT wrote:
KnighT wrote:
Stephany Young wrote:
And as soon as the log.write("Service started ") the OnStart event handler
ends and your timer ceases to exist!!!!!!!!!
>
Because ... you have declared as local to the event handler!
>
Move it (and your state object) to class scope or higher and you should be
in business.
>
Private m_timer As Timer = Nothing
Private m_myTimer As New TimerState
>
Protected Overrides Sub OnStart(ByVal args() As String)
>
m_timer = New Timer(AddressOf checkDatabases, m_myTimer, 10000, 60000)
>
log.write("Service started ")
>
End Sub
>
Note also that, in VB.NET, you don't have to instantiate a delegate for the
TimerCallback. You can use AddressOf <methoddirectly.
>
Now for an anomily. Your comment says 'Create a timer that waits 0 seconds,
then invokes every 60 seconds.' but the duetime value specified in the
constructor is 10000 which indicates 10 seconds. As writ, the Timer will
'fire' after 10 seconds and then every 60 seconds after that.
>
>
"KnighT" <br**********@yahoo.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
I have a .net service that runs a System.Threading.Timer. The delegate
points to the function that the service should execute when the timer
elapses. Problem: The timer is not ticking.

I have never used this timer. I used the documentation from Microsoft
as a guide, but I cannot get this timer to work.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim myTimer As New TimerState()

' Create the delegate that invokes methods for the timer.
Dim timerDelegate As New TimerCallback(AddressOf
checkDatabases)

' Create a timer that waits 0 seconds, then invokes every 60
seconds.
Dim timer As New Timer(timerDelegate, myTimer, 10000, 60000)

log.write("Service started ")

End Sub

The log.write("Service....") executes properly. Why is the timer not
working ?
Thank you Stephany, I will give that a try. That makes sense and
hopefully is the solution I am looking for. I was just playing around
with the due time and did not change the comment.

This timer never works. I created a simple windows form to test the
timer and that ticks. The timer in my service never does what it is
supposed to do.

Even if the scope from my first post was incorrect, then it should
still call the delegate and execute the method checkDatabases AT LEAST
ONCE during the onStart(). But it never calls the delegate. WHY ?????
Source code is supposed to do it's job.
Ok - I have now found part of the problem. The method call to
checkDatabases is not calling. So, I commented out all of the code in
the method and placed a simple log.write(). This wrote a line to the
log at every tick. When I uncomment the code, the timer never calls
the method.

Are there restrictions or limitations to the code you are allowed to
put into this delegate ? This just really makes no sense. Something
in the function must be stopping the method call. Services suck.

Dec 14 '06 #6
KnighT wrote:
Ok - I have now found part of the problem. The method call to
checkDatabases is not calling. So, I commented out all of the code in
the method and placed a simple log.write(). This wrote a line to the
log at every tick. When I uncomment the code, the timer never calls
the method.
That makes no sense at all. Do you have exception handling in that
method? Perhaps it is throwing an exception. Check the Application
logs to see if it logged an exception or use try/catch to see if an
exception is occurring.

Dec 14 '06 #7
KnighT wrote:
Ok - I have now found part of the problem. The method call to
checkDatabases is not calling. So, I commented out all of the code in
the method and placed a simple log.write(). This wrote a line to the
log at every tick. When I uncomment the code, the timer never calls
the method.
If, for any reason, the JIT compiler can't load your timer method
(missing Assemblies or methods at runtime), the timer code simply isn't
called - and there's no log of the failure anywhere that I can find!

This is one reason I /don't/ use a Timer to drive a Service, only to
start it, as in:

Sub OnStart()
' Use the timer to side-step the call-and-return stack, to keep
' the Service Control Manager happy
tmrGo.Start()
End Sub

Sub tmrGo_Elapsed/Tick/whatever
' Kill the timer, which we don't need any more
tmrGo.Stop()

' Run the main process
Me.Run()

End Sub

Sub Run()
Do While Not ShutDownRequested()
Try

DoWork()

Catch ex As Exception

LogError( ex.ToString() )

End Try

For i As Integer = 1 to 30
If ShutDownRequested() Then Exit For
System.Threading.Thread.Sleep( 1000 )
Next
Loop

End Sub
Are you sure
Are there restrictions or limitations to the code you are allowed to
put into this delegate ? This just really makes no sense. Something
in the function must be stopping the method call. Services suck.
Dec 15 '06 #8

Phill W. wrote:
KnighT wrote:
Ok - I have now found part of the problem. The method call to
checkDatabases is not calling. So, I commented out all of the code in
the method and placed a simple log.write(). This wrote a line to the
log at every tick. When I uncomment the code, the timer never calls
the method.

If, for any reason, the JIT compiler can't load your timer method
(missing Assemblies or methods at runtime), the timer code simply isn't
called - and there's no log of the failure anywhere that I can find!

This is one reason I /don't/ use a Timer to drive a Service, only to
start it, as in:

Sub OnStart()
' Use the timer to side-step the call-and-return stack, to keep
' the Service Control Manager happy
tmrGo.Start()
End Sub

Sub tmrGo_Elapsed/Tick/whatever
' Kill the timer, which we don't need any more
tmrGo.Stop()

' Run the main process
Me.Run()

End Sub

Sub Run()
Do While Not ShutDownRequested()
Try

DoWork()

Catch ex As Exception

LogError( ex.ToString() )

End Try

For i As Integer = 1 to 30
If ShutDownRequested() Then Exit For
System.Threading.Thread.Sleep( 1000 )
Next
Loop

End Sub
Are you sure
Are there restrictions or limitations to the code you are allowed to
put into this delegate ? This just really makes no sense. Something
in the function must be stopping the method call. Services suck.
Well I finally figured it out....almost. The problem was that I
thought that the .exe file that .net compiled was self dependant. So I
never put any of the .dll files that I referenced in the project.

The thing that was really throwing me off was the lack of exception
being thrown. But I was able to see that it stopped executing the
source code at the declarations (Without throwing an exception). It
would just stop. After putting in the .dll files it went through the
entire method.

The second problem was correctly pointed out from the help of you guys.
This was instantiating the timer constructor in the OnStart() method.
The timer would tick once, sometimes twice, and rarely more than 4 or 5
times, then just stop.

So I put this timer in onStart(): Dim timer As New
Timer(timerDelegate, myTimer, 10000, 0)

which calls this method:

Sub startMainTimer(ByVal state As Object)
' Create a timer that waits one second, then invokes every 60
seconds.
log.write("Start main timer")
Dim timer As New Timer(AddressOf checkDatabases, myTimer,
10000, 60000)

' Keep a handle to the timer
myTimer.tmr = timer

While (Not myTimer.tmr Is Nothing)

End While

End Sub

So now my timer is ticking every minute and it is properly executing
the source code in the checkDatabases() method.

New problem: lots of errors from that method, which is uploading
databases. Which is strange because I have form that is doing the
exact same thing as the service and it works properly.

Anyways, thanks for your help. I received some very good advice that
helped alot.

Dec 15 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ELO | last post by:
Hi all Every week, I need to get two files on a remote server. I have developped a C# Windows Service with two System.Threading.Timer to do this task For the first one, the delay (TimeSpan...
4
by: Hagay Lupesko | last post by:
Hi, I've encountered a strange phenomena which appears to me as a bug: I have an engine that uses a System.Threading.Timer to invoke a delegate every X minutes. The code looks something...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
2
by: steve | last post by:
Since System.Threading.Timer uses the threadpool to do its stuff what happens when (a) You try to create a timer and the thread pool is *exhausted* (b) The timer is due to fire AND all threads...
0
by: Kelsang Wangchuk | last post by:
Hi Just a quick question... When would you use System.Timers.Timer, and when System.Threading.Timer? What are the principal differences between them? There is a lot of discussion about...
0
by: Bruce | last post by:
Hi I have a question on using System.Threading.Timer. I understand that in order for the timer to work, I need to store a reference to the timer so that it does not get garbage collected. In my...
4
by: Lauren Quantrell | last post by:
I have just put together a vb.net app and now need to provide it to users. This application needs to run the code in a sub every 60 seconds from a Windows Service application. I have the...
9
by: MimiMi | last post by:
I want to perform certain tasks at certain intervals (update a GUI). But I also want to be able to update the GUI 'manually' (i.e a button click). How is this best achieved? I've tried to use a...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.