Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.Threading.Thread.Sleep() not working

System.Threading.Thread.Sleep() not working

Scheduled Pinned Locked Moved C#
csshelpquestionworkspace
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aei_totten
    wrote on last edited by
    #1

    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 this Using 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

    L C D L 4 Replies Last reply
    0
    • A aei_totten

      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 this Using 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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      aei_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

      1 Reply Last reply
      0
      • A aei_totten

        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 this Using 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

        C Offline
        C Offline
        ChrisKo 0
        wrote on last edited by
        #3

        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[^]

        L 1 Reply Last reply
        0
        • A aei_totten

          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 this Using 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

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Windows 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

          1 Reply Last reply
          0
          • C ChrisKo 0

            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[^]

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

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


            1 Reply Last reply
            0
            • A aei_totten

              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 this Using 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

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

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


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups