Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I need some high accuracy Wait/Sleep/Pauses for an application i'm writing. Currently, I am using an AutoResetEvent in wait mode to pull this off so that I can break the Wait w/out aborting a thread.

 

I can't seem to pull off a Pause with a higher accuracy than 10ms. This is the case even when I use thread.sleep to do the pause.

 

So, currently, using either of those methods if i set the wait time anywhere between 0-10ms , it will be a wait time of EITHER 0 OR 10 ms. The pauses have a resolution of 10ms. If the wait is set to 15ms, it will either wait 10 or 20ms.

 

I have tried using the 'timeBeginPeriod' and 'timeEndPeriod' API's to increase the Pause resolution, but it doesnt seem to work. This may be because thread.sleep and AutoresetEvent.wait uses the OS Kernel Waitable, while the 'timeBeginPeriod' and 'timeEndPeriod' work on the multimedia timer service. But, it seemed to work correctly for this guy: http://www.dotnet247.com/247reference/msgs/57/289291.aspx

 

If you can help me Implement this strategy better, or provide another one that'd be great. I'd like a Pause/Sleep that is Breakable, with a resoltion of about 1ms

 

Here is what I currently Have (Both methods Wait for either 0 or 10ms, but nothing inbetween):

'API Declarations
   Public Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal period As Integer) As Integer
   Public Declare Function timeEndPeriod Lib "winmm.dll" (ByVal period As Integer) As Integer

 

Private Sub Test()
  Dim TestThread As New Thread(AddressOf TestFuncThread)
  TestThread.IsBackground = True
  TestThread.Start()
End Sub


   Private Sub TestFuncThread()
       Dim ARE As New AutoResetEvent(False)
       Dim StartTime As New Date
       Dim EndTime As New Date

       'Test Thread.Sleep Non-Breakable Sleep
       StartTime = Now
       timeBeginPeriod(1)
       Thread.Sleep(5)
       timeEndPeriod(1)
       EndTime = Now
       Debug.WriteLine("Actual Thread.Sleep (ms):" + EndTime.Subtract(StartTime).TotalMilliseconds.ToString)

       'Test AutoResetEvent Breakable Wait
       StartTime = Now
       ARE.Reset()
       timeBeginPeriod(1)
       ARE.WaitOne(5, False)
       timeEndPeriod(1)
       EndTime = Now
       Debug.WriteLine("Actual AutoResetEvent Wait (ms):" + EndTime.Subtract(StartTime).TotalMilliseconds.ToString)

   End Sub

Edited by NeuralJack
Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
  • 2 months later...

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...