System.Threading.Thread.Sleep() not working
-
Hey I am trying to use System.Threading.Thread.Sleep() and I am not getting expected results. I am trying to use it as a delay
turn RTS on System.Threading.Thread.Sleep(5); turn RTS off
I have an o-scope hooked to the RTS line and I would expect that the RTS should go on and off anytime over 5 milliseconds (due to windows timing issues), but I would not expect to see it take anytime less than 5 milliseconds. However, I am seeing results anywhere from just under 1 millisecond up to 20 milliseconds. Again if it was just 4 milliseconds (because of the resolution, i thought maybe we could see something as long as it was greater than 4) to whatever, I would be fine with that, but it is returning in less than 4 milliseconds down to one millisecond even! Just for giggles I set up thisUsing System.Diagnostics; Using System.Threading; long startTime,finishTime,diff; float elapsed; for (int x = 1; x <=10 ; x++) { startTime = Stopwatch.GetTimeStamp(); Thread.Sleep(x); finishTime = Stopwatch.GetTimeStamp(); diff = finishTime - startTime; elapsed = diff / 10000; Console.Write(x + " > " + elapsed + "ms > " + diff); }
And am seeing very similar (and stragne) results. :confused: Is this just a bug in the the sleep function or what? Is there another function that I can call instead? (Don't even think about writing that I should be polling---don't get me started--, I am working in an environment where milliseconds difference are noticable, polling would be such a time killer) Thanks Brandy -
Hey I am trying to use System.Threading.Thread.Sleep() and I am not getting expected results. I am trying to use it as a delay
turn RTS on System.Threading.Thread.Sleep(5); turn RTS off
I have an o-scope hooked to the RTS line and I would expect that the RTS should go on and off anytime over 5 milliseconds (due to windows timing issues), but I would not expect to see it take anytime less than 5 milliseconds. However, I am seeing results anywhere from just under 1 millisecond up to 20 milliseconds. Again if it was just 4 milliseconds (because of the resolution, i thought maybe we could see something as long as it was greater than 4) to whatever, I would be fine with that, but it is returning in less than 4 milliseconds down to one millisecond even! Just for giggles I set up thisUsing System.Diagnostics; Using System.Threading; long startTime,finishTime,diff; float elapsed; for (int x = 1; x <=10 ; x++) { startTime = Stopwatch.GetTimeStamp(); Thread.Sleep(x); finishTime = Stopwatch.GetTimeStamp(); diff = finishTime - startTime; elapsed = diff / 10000; Console.Write(x + " > " + elapsed + "ms > " + diff); }
And am seeing very similar (and stragne) results. :confused: Is this just a bug in the the sleep function or what? Is there another function that I can call instead? (Don't even think about writing that I should be polling---don't get me started--, I am working in an environment where milliseconds difference are noticable, polling would be such a time killer) Thanks Brandyaei_totten wrote:
Is this just a bug in the the sleep function or what?
I don't believe so. I believe it is documented to have inconsistent results for anything less that 55 mills. Granted I haven't looked at it for many years but I think that's what it said at one time. I also believe that are higher resolution timers[^] you should use for the type of requirements you have.
led mike
-
Hey I am trying to use System.Threading.Thread.Sleep() and I am not getting expected results. I am trying to use it as a delay
turn RTS on System.Threading.Thread.Sleep(5); turn RTS off
I have an o-scope hooked to the RTS line and I would expect that the RTS should go on and off anytime over 5 milliseconds (due to windows timing issues), but I would not expect to see it take anytime less than 5 milliseconds. However, I am seeing results anywhere from just under 1 millisecond up to 20 milliseconds. Again if it was just 4 milliseconds (because of the resolution, i thought maybe we could see something as long as it was greater than 4) to whatever, I would be fine with that, but it is returning in less than 4 milliseconds down to one millisecond even! Just for giggles I set up thisUsing System.Diagnostics; Using System.Threading; long startTime,finishTime,diff; float elapsed; for (int x = 1; x <=10 ; x++) { startTime = Stopwatch.GetTimeStamp(); Thread.Sleep(x); finishTime = Stopwatch.GetTimeStamp(); diff = finishTime - startTime; elapsed = diff / 10000; Console.Write(x + " > " + elapsed + "ms > " + diff); }
And am seeing very similar (and stragne) results. :confused: Is this just a bug in the the sleep function or what? Is there another function that I can call instead? (Don't even think about writing that I should be polling---don't get me started--, I am working in an environment where milliseconds difference are noticable, polling would be such a time killer) Thanks Brandy -
Hey I am trying to use System.Threading.Thread.Sleep() and I am not getting expected results. I am trying to use it as a delay
turn RTS on System.Threading.Thread.Sleep(5); turn RTS off
I have an o-scope hooked to the RTS line and I would expect that the RTS should go on and off anytime over 5 milliseconds (due to windows timing issues), but I would not expect to see it take anytime less than 5 milliseconds. However, I am seeing results anywhere from just under 1 millisecond up to 20 milliseconds. Again if it was just 4 milliseconds (because of the resolution, i thought maybe we could see something as long as it was greater than 4) to whatever, I would be fine with that, but it is returning in less than 4 milliseconds down to one millisecond even! Just for giggles I set up thisUsing System.Diagnostics; Using System.Threading; long startTime,finishTime,diff; float elapsed; for (int x = 1; x <=10 ; x++) { startTime = Stopwatch.GetTimeStamp(); Thread.Sleep(x); finishTime = Stopwatch.GetTimeStamp(); diff = finishTime - startTime; elapsed = diff / 10000; Console.Write(x + " > " + elapsed + "ms > " + diff); }
And am seeing very similar (and stragne) results. :confused: Is this just a bug in the the sleep function or what? Is there another function that I can call instead? (Don't even think about writing that I should be polling---don't get me started--, I am working in an environment where milliseconds difference are noticable, polling would be such a time killer) Thanks BrandyWindows is not a real-time O/S. The resolution of those timers is not guaranteed and can vary from system to system, and even vary due to system load. If you want to grab analog/digital data at a consistance rate, you'd have to use hardware dedicated to the task. This would be something like an Analog/Digital converter board or some other PCI/PCIx data acquisition board that comes with an API to do the data capture/buffering independant of the system CPU.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
The .NET System timers are highly inaccurate at anything in the 1-5ms range. Please see this great article by Luc Pattyn. http://www.codeproject.com/KB/cs/LP_TimerTest.aspx[^]
:rose:
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hey I am trying to use System.Threading.Thread.Sleep() and I am not getting expected results. I am trying to use it as a delay
turn RTS on System.Threading.Thread.Sleep(5); turn RTS off
I have an o-scope hooked to the RTS line and I would expect that the RTS should go on and off anytime over 5 milliseconds (due to windows timing issues), but I would not expect to see it take anytime less than 5 milliseconds. However, I am seeing results anywhere from just under 1 millisecond up to 20 milliseconds. Again if it was just 4 milliseconds (because of the resolution, i thought maybe we could see something as long as it was greater than 4) to whatever, I would be fine with that, but it is returning in less than 4 milliseconds down to one millisecond even! Just for giggles I set up thisUsing System.Diagnostics; Using System.Threading; long startTime,finishTime,diff; float elapsed; for (int x = 1; x <=10 ; x++) { startTime = Stopwatch.GetTimeStamp(); Thread.Sleep(x); finishTime = Stopwatch.GetTimeStamp(); diff = finishTime - startTime; elapsed = diff / 10000; Console.Write(x + " > " + elapsed + "ms > " + diff); }
And am seeing very similar (and stragne) results. :confused: Is this just a bug in the the sleep function or what? Is there another function that I can call instead? (Don't even think about writing that I should be polling---don't get me started--, I am working in an environment where milliseconds difference are noticable, polling would be such a time killer) Thanks Brandyaei_totten wrote:
Thread.Sleep() not working
Of course, it is Thread.Sleep(), it isn't Thread.Work(). :-D
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.