L
Lauren Quantrell
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 functionality
of the sub working fine but I cannot figure out how to run the timer.
The sub DoSomethingHere() needs to run every 60 seconds. It doesn't
need to change the interval or anything like that. It just needs to
run every 60 seconds as long as the service is running.
I have never used a timer and my attempts to make this work haven't
worked.
I'm hoping someone can show me how to do this in a Windows Service app
in vb.net.
Much Thanks!
lq
System.Threading.Timer code from MSDN:
'Start Code:
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading
Public Class TimerExample
Shared Sub Main()
Dim autoEvent As New AutoResetEvent(False)
Dim statusChecker As New StatusChecker(10)
' Create the delegate that invokes methods for the timer.
Dim timerDelegate As TimerCallback = _
AddressOf statusChecker.CheckStatus
' Create a timer that signals the delegate to invoke
' CheckStatus after one second, and every 1/4 second
' thereafter.
Console.WriteLine("{0} Creating timer." & vbCrLf, _
DateTime.Now.ToString("h:mm:ss.fff"))
Dim stateTimer As Timer = _
New Timer(timerDelegate, autoEvent, 1000, 250)
' When autoEvent signals, change the period to every
' 1/2 second.
autoEvent.WaitOne(5000, False)
stateTimer.Change(0, 500)
Console.WriteLine(vbCrLf & "Changing period." & vbCrLf)
' When autoEvent signals the second time, dispose of
' the timer.
autoEvent.WaitOne(5000, False)
stateTimer.Dispose()
Console.WriteLine(vbCrLf & "Destroying timer.")
End Sub
End Class
Public Class StatusChecker
Dim invokeCount, maxCount As Integer
Sub New(count As Integer)
invokeCount = 0
maxCount = count
End Sub
' This method is called by the timer delegate.
Sub CheckStatus(stateInfo As Object)
Dim autoEvent As AutoResetEvent = _
DirectCast(stateInfo, AutoResetEvent)
invokeCount += 1
Console.WriteLine("{0} Checking status {1,2}.", _
DateTime.Now.ToString("h:mm:ss.fff"), _
invokeCount.ToString())
If invokeCount = maxCount Then
' Reset the counter and signal to stop the timer.
invokeCount = 0
autoEvent.Set()
End If
End Sub
End Class
users. This application needs to run the code in a sub every 60
seconds from a Windows Service application. I have the functionality
of the sub working fine but I cannot figure out how to run the timer.
The sub DoSomethingHere() needs to run every 60 seconds. It doesn't
need to change the interval or anything like that. It just needs to
run every 60 seconds as long as the service is running.
I have never used a timer and my attempts to make this work haven't
worked.
I'm hoping someone can show me how to do this in a Windows Service app
in vb.net.
Much Thanks!
lq
System.Threading.Timer code from MSDN:
'Start Code:
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading
Public Class TimerExample
Shared Sub Main()
Dim autoEvent As New AutoResetEvent(False)
Dim statusChecker As New StatusChecker(10)
' Create the delegate that invokes methods for the timer.
Dim timerDelegate As TimerCallback = _
AddressOf statusChecker.CheckStatus
' Create a timer that signals the delegate to invoke
' CheckStatus after one second, and every 1/4 second
' thereafter.
Console.WriteLine("{0} Creating timer." & vbCrLf, _
DateTime.Now.ToString("h:mm:ss.fff"))
Dim stateTimer As Timer = _
New Timer(timerDelegate, autoEvent, 1000, 250)
' When autoEvent signals, change the period to every
' 1/2 second.
autoEvent.WaitOne(5000, False)
stateTimer.Change(0, 500)
Console.WriteLine(vbCrLf & "Changing period." & vbCrLf)
' When autoEvent signals the second time, dispose of
' the timer.
autoEvent.WaitOne(5000, False)
stateTimer.Dispose()
Console.WriteLine(vbCrLf & "Destroying timer.")
End Sub
End Class
Public Class StatusChecker
Dim invokeCount, maxCount As Integer
Sub New(count As Integer)
invokeCount = 0
maxCount = count
End Sub
' This method is called by the timer delegate.
Sub CheckStatus(stateInfo As Object)
Dim autoEvent As AutoResetEvent = _
DirectCast(stateInfo, AutoResetEvent)
invokeCount += 1
Console.WriteLine("{0} Checking status {1,2}.", _
DateTime.Now.ToString("h:mm:ss.fff"), _
invokeCount.ToString())
If invokeCount = maxCount Then
' Reset the counter and signal to stop the timer.
invokeCount = 0
autoEvent.Set()
End If
End Sub
End Class