VB Windows Service

  • Thread starter Thread starter Doug Stiers
  • Start date Start date
D

Doug Stiers

I have a VB app that I'm installing as a Windows Service. I want a
subroutine in the app to run every 30 minutes during business hours. How do
I do this in VB? I set the startup type as automatic so it's always running.
Is there some kind of timer event I can use?

Thanks,
Doug
 
¤ I have a VB app that I'm installing as a Windows Service. I want a
¤ subroutine in the app to run every 30 minutes during business hours. How do
¤ I do this in VB? I set the startup type as automatic so it's always running.
¤ Is there some kind of timer event I can use?
¤

Use the System.Threading.Timer class:

http://msdn.microsoft.com/library/d.../html/frlrfsystemthreadingtimerclasstopic.asp


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Doug,
As Paul stated, you can use either the System.Threading.Timer class or the
System.Timers.Timer class.

http://msdn.microsoft.com/library/d...ref/html/frlrfsystemtimerstimerclasstopic.asp

The advantage of the Threading.Timer is it has a "due time" parameter on
when the first event should occur.

I've using System.Timers.Timer in my services with success.

The following article in MSDN Magazine explains the difference between the
three timer objects in .NET & when to use each.

http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx

The above article also discusses if & how each timer interacts with
threading.


I would consider using a TimeRange structure to only process events during
"business hours", possibly reading "business hours" form the app.config
file.

For information on a TimeRange structure that I created see:
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#835f79ef3acde155

Static businessHours As New TimeRange(#8:00:00 AM#, #5:00:00 PM#)

If businessHours.Contains(DateTime.Now) Then
' process the event
Else
' skip the event
End If

Hope this helps
Jay
 
¤ Doug,
¤ As Paul stated, you can use either the System.Threading.Timer class or the
¤ System.Timers.Timer class.
¤
¤ http://msdn.microsoft.com/library/d...ref/html/frlrfsystemtimerstimerclasstopic.asp
¤
¤ The advantage of the Threading.Timer is it has a "due time" parameter on
¤ when the first event should occur.
¤
¤ I've using System.Timers.Timer in my services with success.
¤
¤ The following article in MSDN Magazine explains the difference between the
¤ three timer objects in .NET & when to use each.
¤
¤ http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx
¤
¤ The above article also discusses if & how each timer interacts with
¤ threading.
¤
¤
¤ I would consider using a TimeRange structure to only process events during
¤ "business hours", possibly reading "business hours" form the app.config
¤ file.
¤
¤ For information on a TimeRange structure that I created see:
¤ http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#835f79ef3acde155
¤
¤ Static businessHours As New TimeRange(#8:00:00 AM#, #5:00:00 PM#)
¤
¤ If businessHours.Contains(DateTime.Now) Then
¤ ' process the event
¤ Else
¤ ' skip the event
¤ End If
¤
¤ Hope this helps
¤ Jay


Good info Jay. You just need to be aware of the following bug when using System.Timers.Timer:

BUG: The Elapsed event of the System.Timers.Timer class is not raised in a Windows service
http://support.microsoft.com/default.aspx?scid=kb;en-us;842793


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Paul,
I've read that KB and it appears to be a non-issue for me.

I only stop the timer in the Service's OnStop method, for this service when
OnStop is called the service is unloaded from memory, as the executable only
has a single Service object in it.

I do use Timer.AutoReset = False so the elapsed event only occurs once, my
elapsed event handler then decides what the next Timer.Interval is and then
sets Timer.Enable = True

Now I'm curious if AutoReset = False might the same problem. I've been
running my service for months and it has not stopped unexpectedly... I'll
sneak a GC.Collect in there just to verify it...

Truthfully with Timer.AutoReset, I'm not sure when I would ever use
Timer.Stop in the Elapsed event handler. Also wouldn't simply creating a new
Timer Object in the Elapsed event handler be "easier" then remunging your
code to get Threading.Timer to work?

Thanks for the reminder.

Hope this helps
Jay


Paul Clement said:
On Tue, 7 Dec 2004 13:53:06 -0600, "Jay B. Harlow [MVP - Outlook]"

¤ Doug,
¤ As Paul stated, you can use either the System.Threading.Timer class or
the
¤ System.Timers.Timer class.
¤
¤
http://msdn.microsoft.com/library/d...ref/html/frlrfsystemtimerstimerclasstopic.asp
¤
¤ The advantage of the Threading.Timer is it has a "due time" parameter on
¤ when the first event should occur.
¤
¤ I've using System.Timers.Timer in my services with success.
¤
¤ The following article in MSDN Magazine explains the difference between
the
¤ three timer objects in .NET & when to use each.
¤
¤ http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx
¤
¤ The above article also discusses if & how each timer interacts with
¤ threading.
¤
¤
¤ I would consider using a TimeRange structure to only process events
during
¤ "business hours", possibly reading "business hours" form the app.config
¤ file.
¤
¤ For information on a TimeRange structure that I created see:
¤
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#835f79ef3acde155
¤
¤ Static businessHours As New TimeRange(#8:00:00 AM#, #5:00:00 PM#)
¤
¤ If businessHours.Contains(DateTime.Now) Then
¤ ' process the event
¤ Else
¤ ' skip the event
¤ End If
¤
¤ Hope this helps
¤ Jay


Good info Jay. You just need to be aware of the following bug when using
System.Timers.Timer:

BUG: The Elapsed event of the System.Timers.Timer class is not raised in a
Windows service
http://support.microsoft.com/default.aspx?scid=kb;en-us;842793


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Chanmm,
As you know, the scheduler in control panel won't really help with a Windows
Service! (per se)

The scheduler in control panel will however help with a Console or Windows
application.

Hope this helps
Jay
 
Back
Top