microkarl Posted December 27, 2005 Posted December 27, 2005 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): 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. Quote Donald DUCK : YOU ARE FIRED!!!
HJB417 Posted December 27, 2005 Posted December 27, 2005 Use synchronization. If a method needs to be synchronized, I usually use the MethodImplAttribute Class. E.x.: 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 Quote
HJB417 Posted December 27, 2005 Posted December 27, 2005 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. Quote
microkarl Posted December 27, 2005 Author Posted December 27, 2005 How about a Monitor Class then, use Monitor.TryEnter() and Monitor.Exit() Quote Donald DUCK : YOU ARE FIRED!!!
HJB417 Posted December 27, 2005 Posted December 27, 2005 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. Quote
microkarl Posted December 28, 2005 Author Posted December 28, 2005 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. Quote Donald DUCK : YOU ARE FIRED!!!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.