microkarl
Regular
All,
I have a question on using Threading Timer. Basically this is my first time to use this timer and before this, I use System.Timers most of the time (well, until I found out using System.Timers would be a pain in Windows Service...)
unlike Systems.Timer, Threading.Timer does not Start() and Stop() methods, and the worst of all, Threading Timer is NOT thread-safe, and allow Re-Entrence, how can I gurantee to all my code begin execute inside the TimerCallBack() without re-entrance? Is there any way to set the timer stop inside the TimerCallBack.
Here is one example (PseudocodeCode):
Thanks.
I have a question on using Threading Timer. Basically this is my first time to use this timer and before this, I use System.Timers most of the time (well, until I found out using System.Timers would be a pain in Windows Service...)
unlike Systems.Timer, Threading.Timer does not Start() and Stop() methods, and the worst of all, Threading Timer is NOT thread-safe, and allow Re-Entrence, how can I gurantee to all my code begin execute inside the TimerCallBack() without re-entrance? Is there any way to set the timer stop inside the TimerCallBack.
Here is one example (PseudocodeCode):
Code:
Public Class Main
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim autoEvent As New AutoResetEvent(False)
Dim mTimer As New TimerThread
Dim mtimerDelegate As TimerCallback = AddressOf mTimer.OnCheckMessages
stateTimer = New Timer(mtimerDelegate, autoEvent, 0, 2000)
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
End Try
End Sub
End Class
Public Class TimerThread
Public Sub New()
End Sub
' This method is called by the timer delegate.
Public Sub OnCheckMessages(ByVal stateInfo As Object)
Dim FileName As String
Dim fileEntries As String()
Dim autoEvent As AutoResetEvent
Try
'What can I do here to make sure all the code being exectued without being interruptted.
objLog = New ProcessLogging
autoEvent = DirectCast(stateInfo, AutoResetEvent)
fileEntries = mDir.GetFiles("C:\Temp\", "*.txt")
For Each FileName in fileEntries
'TO DO: Write the name to a log file....
Next
Catch ex As Exception
'TO DO...
End Try
End Sub
End Class
Thanks.