Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System.Threading.timer(stuff, N, Junk) - not working

Status
Not open for further replies.
Jul 19, 2005
3
US
Hey all-
I am working on my first windows service and I am having trouble using the System.Threading.Timer Class. Could someone give me a little help. I just want it to fire off every 5-10 seconds.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim strTime As String = ConfigurationManager.AppSettings("ExportTimer").ToString
Dim myCallBack As TimerCallback = New TimerCallback(AddressOf Me.mySub)
Dim tExport As Timer = New Timer(myCallBack, 0, 0, CInt(strTime))

End Sub
Public Sub mySub(ByVal state As Object)
Call CreateXMLRegistry()
End Sub
 
at some point you have to start and stop the timer.
tExport.Start
tExport.Stop
 
asp,

In creating the Timer class, you need to specify the stat object (which can be null if you don't need one), duration prior to the initial poll, and then the duration between polls.

For instance, if you wanted to start polling immediately, and then again every second, you'd write:

tExport = new System.Threading.Timer(myCallback, null, 0, 1000);

To begin the first poll in 1/2 second, and then every 1/4 sec afterwards, it would be:

tExport = new System.Threading.Timer(myCallback, null, 500, 250);


Hope this helps.
-Greg
 
One other thing: System.Windows.Forms.Timer has a start/stop method. System.Threading.Timer does not.

You can change the duration between polls by calling tExport.Change() if you wish.

To Stop the timer, simply dispose it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top