Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Donald DUCK : YOU ARE FIRED!!!
Posted

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

Posted
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.
Posted
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.
Donald DUCK : YOU ARE FIRED!!!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...