473,385 Members | 1,830 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,385 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 6822
>
' 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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.