6

The following function System.Threading.Thread.Sleep(); delay the thread in millisecond, and take the integer value as a parameter. Is there any method of thread delay in microsecond. Or can sleep function take the float values? Thanks

Siddiqui
  • 7,662
  • 17
  • 81
  • 129
  • Providing a meaningful answer to this question would be easier if you stated why you need to sleep for such short periods. Perhaps there is a different approach to accomplish what you need in a way that doesn't require any calls to Sleep(). – LBushkin May 25 '09 at 03:43
  • [look at what I said in another question about stopwatch](http://stackoverflow.com/questions/1631501/need-microsecond-delay-in-net-app-for-throttling-udp-multicast-transmission-rate/1631542#1631542) – Fredou Nov 04 '09 at 02:21
  • didn't see it was a question from May :-p – Fredou Nov 04 '09 at 02:26

4 Answers4

17

Nope. To quote Will Dean from 85122

you can't meaningfully 'sleep' (i.e. relinquish your scheduled CPU) for such short periods. If you want to delay for some short time, then you need to spin, repeatedly checking a suitably high-resolution timer (e.g. the 'performance timer') and hoping that something of high priority doesn't pre-empt you anyway.

Community
  • 1
  • 1
Dan F
  • 11,958
  • 3
  • 48
  • 72
10

Sleep is not accurate to the millisecond in the first place. I think it's resolution is hardware dependent, and the smallest slice of time you can sleep will typically be around 20 ms. This has to do with Sleep actually releases what might be remaining of the current thread's timeslice; and allows another thread to run. The earliest time your thread will be able to run again, is after one thread scheduler timeslice has passed. Therefore the resolution is about 20 ms (assuming a timeslice on your system is 20 ms).

Since Windows is not a real-time OS; Sleep and other wait functions can never be fully deterministic.

Depending on your application, you could perhaps do a Sleep for the most of the time and then do a busy wait where timing is critical. Or rewrite the structure so that you measure time passed accurately (use System.Diagnostics.Stopwatch); but do not depend on being able to sleep for an accurate time period in the millisecond range.

The first answer in this thread on MSDN also explains it very nicely.

driis
  • 161,458
  • 45
  • 265
  • 341
1

The best granularity you will find for Thread.Sleep is using the TimeSpan class. Unfortunately the smallest granularity it supports is milliseconds. There is no way to sleep in the microseconds range.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

I had to write a music program where the notes had to start/stop at the correct duration, precisely, so I used the high-resolution timer in DirectX to do this, but, my application would also stay on top and hog the cpu, so it could keep checking for when to do the next action.

Since there is no way to know exactly when the OS will return control to your application, Thread.Sleep will be a very bad choice, and, as was mentioned, the smallest time increment can vary between 10 and 50ms, in my experience.

James Black
  • 41,583
  • 10
  • 86
  • 166