NeuralJack Posted April 16, 2007 Posted April 16, 2007 (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 April 16, 2007 by NeuralJack Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
NeuralJack Posted June 29, 2007 Author Posted June 29, 2007 Anyone happen to know a way to fix this? Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Administrators PlausiblyDamp Posted June 30, 2007 Administrators Posted June 30, 2007 http://www.xtremedotnettalk.com/showthread.php?t=100425 has some possible solutions. Also http://www.microsoft.com/whdc/system/CEC/mm-timer.mspx discusses some of the different timers provided by windows. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NeuralJack Posted July 4, 2007 Author Posted July 4, 2007 Thanks, i hadnt seen these links. Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
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.