A question on System.Threading.Timer

microkarl

Regular
Joined
Apr 22, 2004
Messages
88
Location
Morristown, NJ
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):
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.
 
Use synchronization.
If a method needs to be synchronized, I usually use the MethodImplAttribute Class.

E.x.:
Visual Basic:
Imports System
Imports System.Runtime.CompilerServices

Class Class1

    <STAThread()> _
    Shared Sub Main(ByVal args As String())
    End Sub

    'The following function is re-entrant safe
    <MethodImpl(MethodImplOptions.Synchronized)> _
    Sub AReEntrenceSafeFunction()
        'do stuff
    End Sub

End Class
 
FYI, the current implementation of MethodImplOptions.Synchronized uses a SyncLock Statement at runtime. The locked object is 'Me' for instance methods, and the Type of the containing class for static aka 'Shared' methods.
 
The Monitor class will work too. The compiler basically replaces the SyncLock statement with calls to the Monitor class, as stated in the documentation for SyncLock.
 
Okay, we can synclock the TimerCallBack() and gurantee that only ONE delegate get to the resource and other (threads) are in queue. However, is there any tricks, or hacks, to gurantee that ONLY ONE delegate fires ALL THE TIME. Because the problem I have is something that synchornization won't not completely solve it. In summary, I need to have the Threading Timer act like a System.Timers.Timer. and that is, only ONE delegate can touch the resources. Please help!! many thanks.
 
Back
Top