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. Thread.Sleep is too slow

Thread.Sleep is too slow

Scheduled Pinned Locked Moved C#
question
11 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.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    I have Thread.Sleep in my program, and to fastest I can get it to sleep is 1 milla second... I need it to be faster... 1 Micro second.... and way to do this? /\ |_ E X E GG

    A D J 3 Replies Last reply
    0
    • E eggie5

      I have Thread.Sleep in my program, and to fastest I can get it to sleep is 1 milla second... I need it to be faster... 1 Micro second.... and way to do this? /\ |_ E X E GG

      A Offline
      A Offline
      albean
      wrote on last edited by
      #2

      When Thread.Sleep is executed the .NET framework makes a system call that immediately causes a context switch. (AFAIK) Could you give a little more info about what you are trying to do?

      E 1 Reply Last reply
      0
      • E eggie5

        I have Thread.Sleep in my program, and to fastest I can get it to sleep is 1 milla second... I need it to be faster... 1 Micro second.... and way to do this? /\ |_ E X E GG

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        Maybe Thread.SpinWait could help you. More time resolution is only available with multimedia timers or DirectX. Acting as a substitute for God, he becomes a dispenser of justice. - Alexandre Dumas

        E 1 Reply Last reply
        0
        • D Daniel Turini

          Maybe Thread.SpinWait could help you. More time resolution is only available with multimedia timers or DirectX. Acting as a substitute for God, he becomes a dispenser of justice. - Alexandre Dumas

          E Offline
          E Offline
          eggie5
          wrote on last edited by
          #4

          Can it sleep from 1 micro second? /\ |_ E X E GG

          1 Reply Last reply
          0
          • A albean

            When Thread.Sleep is executed the .NET framework makes a system call that immediately causes a context switch. (AFAIK) Could you give a little more info about what you are trying to do?

            E Offline
            E Offline
            eggie5
            wrote on last edited by
            #5

            I just need my program to to pause... or delay... or wait.. for 1 micro second... the Thread.Sleep can only go as fast as 1 milla second... /\ |_ E X E GG

            A L 2 Replies Last reply
            0
            • E eggie5

              I just need my program to to pause... or delay... or wait.. for 1 micro second... the Thread.Sleep can only go as fast as 1 milla second... /\ |_ E X E GG

              A Offline
              A Offline
              albean
              wrote on last edited by
              #6

              If you just want a short "delay" then you do not want to use Thread.Sleep. Because even a Thread.Sleep(0) causes a context switch and it may take "a long time" for the OS to hand control back to your program, depending on how many threads/tasks are scheduled. Perhaps there is a way to inline some assembly in a C# app (I don't know about this though) and you could execute a no-op or something that would fill your requirements for a delay. But you will still have that context switching problem since windows is preemptive multitasking. Consider this: 1 Executing code 2 Executing code 3 Your Delay 4 Executing code 5 Executing code Even if there were some (atomic) delay statement that you could use at step 3 a context switch could take place between 3 and 4. This would make your delay appear to be even longer. Good luck though, maybe there is a workaround.

              E 1 Reply Last reply
              0
              • E eggie5

                I have Thread.Sleep in my program, and to fastest I can get it to sleep is 1 milla second... I need it to be faster... 1 Micro second.... and way to do this? /\ |_ E X E GG

                J Offline
                J Offline
                jparsons
                wrote on last edited by
                #7

                A nasty nasty dirty hack would to get tehc urrent time, then do a while loop that just checks the time difference until 1 micro second has passed. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

                1 Reply Last reply
                0
                • A albean

                  If you just want a short "delay" then you do not want to use Thread.Sleep. Because even a Thread.Sleep(0) causes a context switch and it may take "a long time" for the OS to hand control back to your program, depending on how many threads/tasks are scheduled. Perhaps there is a way to inline some assembly in a C# app (I don't know about this though) and you could execute a no-op or something that would fill your requirements for a delay. But you will still have that context switching problem since windows is preemptive multitasking. Consider this: 1 Executing code 2 Executing code 3 Your Delay 4 Executing code 5 Executing code Even if there were some (atomic) delay statement that you could use at step 3 a context switch could take place between 3 and 4. This would make your delay appear to be even longer. Good luck though, maybe there is a workaround.

                  E Offline
                  E Offline
                  eggie5
                  wrote on last edited by
                  #8

                  ok, sleeping for (1) is one milla second right? well what would (0) be? that's crazy... how long is 0? could that possibly be considered a micro second almost? /\ |_ E X E GG

                  A 1 Reply Last reply
                  0
                  • E eggie5

                    ok, sleeping for (1) is one milla second right? well what would (0) be? that's crazy... how long is 0? could that possibly be considered a micro second almost? /\ |_ E X E GG

                    A Offline
                    A Offline
                    albean
                    wrote on last edited by
                    #9

                    Eggie, You need to read about the Thread.Sleep method and more importantly what a context switch is. Sleep( X ) means the thread will not be scheduled for execution by the operating system for X milliseconds. Sleep(0) causes the thread to be suspended (a context switch) and immediately rescheduled for another time slice. If there are no other threads at your thread’s priority level then it is executed. The bottom line is you will not be able to create a delay to the precision you are looking for. (Well, you might be able to with in-line assembly but a context switch could occur and make the “delay” longer. And, now that I think about it, you will probably have to boost your thread’s priority. ) Sleep(0) is the shortest delay that I can think of but there is no control over its duration. Since there will be other threads waiting, they will all execute their time slices before yours gets the CPU back. I don’t recall what Microsoft has a time slice set at its probably around 50ms so if there are n threads scheduled before yours the delay will be n * ~50ms In the post above Daniel wrote “More time resolution is only available with multimedia timers or DirectX.” He is right. What I’m speaking about is similar to you listening to music or watching a dvd on your computer. If you do anything that uses the CPU a lot, the music or movie becomes "choppy." That is, indirectly, related to the context switches that I’m talking about. Besides these context switches there are other things like interrupts going on(e.g. moving the mouse). Anyway, there is always a workaround for coding problems. Good luck. BTW: You got me curious. Why do you need such a short delay for? Context switch: (this is just a summary, Google around for more details) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/context\_switches.asp Sleep Method: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadclasssleeptopic1.asp

                    E 1 Reply Last reply
                    0
                    • E eggie5

                      I just need my program to to pause... or delay... or wait.. for 1 micro second... the Thread.Sleep can only go as fast as 1 milla second... /\ |_ E X E GG

                      L Offline
                      L Offline
                      LongRange Shooter
                      wrote on last edited by
                      #10

                      why not do a delay like the old way we'd do it in DOS? DoProcess() DoProcess() DelayCode() DoProcess() public void DelayCode() { for (int i=0; 1<1000; i++) continue; } _____________________________________________ The world is a dangerous place.
                      Not because of those that do evil,
                          but because of those who look on and do nothing.

                      1 Reply Last reply
                      0
                      • A albean

                        Eggie, You need to read about the Thread.Sleep method and more importantly what a context switch is. Sleep( X ) means the thread will not be scheduled for execution by the operating system for X milliseconds. Sleep(0) causes the thread to be suspended (a context switch) and immediately rescheduled for another time slice. If there are no other threads at your thread’s priority level then it is executed. The bottom line is you will not be able to create a delay to the precision you are looking for. (Well, you might be able to with in-line assembly but a context switch could occur and make the “delay” longer. And, now that I think about it, you will probably have to boost your thread’s priority. ) Sleep(0) is the shortest delay that I can think of but there is no control over its duration. Since there will be other threads waiting, they will all execute their time slices before yours gets the CPU back. I don’t recall what Microsoft has a time slice set at its probably around 50ms so if there are n threads scheduled before yours the delay will be n * ~50ms In the post above Daniel wrote “More time resolution is only available with multimedia timers or DirectX.” He is right. What I’m speaking about is similar to you listening to music or watching a dvd on your computer. If you do anything that uses the CPU a lot, the music or movie becomes "choppy." That is, indirectly, related to the context switches that I’m talking about. Besides these context switches there are other things like interrupts going on(e.g. moving the mouse). Anyway, there is always a workaround for coding problems. Good luck. BTW: You got me curious. Why do you need such a short delay for? Context switch: (this is just a summary, Google around for more details) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/context\_switches.asp Sleep Method: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadclasssleeptopic1.asp

                        E Offline
                        E Offline
                        eggie5
                        wrote on last edited by
                        #11

                        I'm making a program... well I have a program that sends data though the parallel port and stores it in the memory on my board... The data is just a 50,000 line .xml file with a bunch of Hexidecmial. Everything works fine... except I need that delay between bytes to be more precise... I just downloaded the DirectX SDK, now I have to figure that out... uh... anymore suggestions? /\ |_ E X E GG

                        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