Windows Service Question

J

John Wright

I am creating a windows service that I need to fire off every 4 hours to
check a database for records. If there are records, then I need to call
a web service to process them. After much reading I found out that I needed
to use the Threading.Timer and a call back to fire off. Not a problem.
However, I need this to run every 4 hours (or any value that is set in the
database). I am including some of the code I need help on. I need to check
the database for the timeout value and reset it after I run the checkDB in
case we need to readjust the time between checks. I figured it would be
easier to set the value in a database than to hard code it and recompile
when we need to change the time between checks? Should this check be in
the OnStart or in the CheckDB as the last command to reset the timer's interval
between callsback? Does OnStart fire each time the timer is invoked or only
when the service is started? Any other suggestions would be appreciated
this timer value needs to be able to be changed without a recompile.


John


Private stateTimer As Threading.Timer
Dim oCallBack As New TimerCallback(AddressOf checkDB)

Protected Overrides Sub OnStart(ByVal args() As String)
evEPNMatrixLog.WriteEntry("EPNSync Started started: " & Now)
objDAL = New DALManager
objDAL.ConnectionString = My.Resources.ConnectionString
objDAL.ProviderName = My.Resources.ProviderName
objParam = New DbParameterCollection
stateTimer = New Timer(oCallBack, Nothing, 60000, 60000) --->I need
to set this value from a database

End Sub

Private Sub CheckDB(ByVal state As Object)
'do processing here
'check the database for the next check time and reset the timer
End Sub
 
S

ShaneO

John said:
I am creating a windows service that I need to fire off every 4 hours to
check a database for records.....
John


Private stateTimer As Threading.Timer
Dim oCallBack As New TimerCallback(AddressOf checkDB)

Protected Overrides Sub OnStart(ByVal args() As String)
evEPNMatrixLog.WriteEntry("EPNSync Started started: " & Now)
objDAL = New DALManager
objDAL.ConnectionString = My.Resources.ConnectionString
objDAL.ProviderName = My.Resources.ProviderName
objParam = New DbParameterCollection
stateTimer = New Timer(oCallBack, Nothing, 60000, 60000) --->I
need to set this value from a database

End Sub

Private Sub CheckDB(ByVal state As Object)
'do processing here
'check the database for the next check time and reset the timer
End Sub
I am doing a similar task and have a Service that's running on a number
of systems using the following -

Dim iTmrMinutes As Integer
Dim WithEvents Tmr As New Timers.Timer

Protected Overrides Sub OnStart(ByVal args() As String)
..
..
.....Code here to read Timer Value (in Minutes)...
With Tmr
.Interval = (1000 * 60) * iTmrMinutes
.AutoReset = True
.Enabled = True
End With
..
..
..
ProcessTask()
..
End Sub

Private Sub Tmr_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Tmr.Elapsed

ProcessTask()

End Sub

Private Sub ProcessTask()
..
......Do whatever you want in here....
..
End Sub


The Timer isn't triggered until the Interval has lapsed, so if you want
to perform some task during startup then you need to call it during
"OnStart".

Hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
J

John Wright

Hello ShaneO,
I am doing a similar task and have a Service that's running on a
number of systems using the following -

Dim iTmrMinutes As Integer
Dim WithEvents Tmr As New Timers.Timer
Protected Overrides Sub OnStart(ByVal args() As String)
.
.
....Code here to read Timer Value (in Minutes)...
With Tmr
.Interval = (1000 * 60) * iTmrMinutes
.AutoReset = True
.Enabled = True
End With
.
.
.
ProcessTask()
.
End Sub
Private Sub Tmr_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Tmr.Elapsed

ProcessTask()

End Sub

Private Sub ProcessTask()
.
.....Do whatever you want in here....
.
End Sub
The Timer isn't triggered until the Interval has lapsed, so if you
want to perform some task during startup then you need to call it
during "OnStart".

Hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those
who don't.

This really helps. What I have done is set the interval on the timer to
1 hour 360000 then in the call back I check the database. If the current
system time - last check time is greater than 4 hours I run my process, if
it isn't then I just exit the procedure. When I do a check on the database,
the last thing I do is update the last check date by 4 hours. Now if I need
to change the time between calls, I change the time between checks in the
database and not worry about going back into my code.
 
S

ShaneO

John said:
Hello ShaneO,


This really helps. What I have done is set the interval on the timer to
1 hour 360000 then in the call back I check the database. If the
current system time - last check time is greater than 4 hours I run my
process, if it isn't then I just exit the procedure. When I do a check
on the database, the last thing I do is update the last check date by 4
hours. Now if I need to change the time between calls, I change the
time between checks in the database and not worry about going back into
my code.
I'm glad it was of assistance.

My Service routine also checks for adjustments, however I do it using an
"INI" type file -

1. Service starts and reads all settings into variables, including Timer
delay. It also stores the "Modified" date/time of the INI file.

2. When the Timer fires it checks the Modified date/time of the INI
file. If it's changed then it re-reads the settings and makes whatever
adjustments are needed.

For my purposes I find the above approach simpler because the Service
doesn't need to update anything and only opens the file if it can detect
that something has been altered. It also means I can make changes using
a Text Editor, which also simplifies the process for my purposes.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top