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