You can't put a thread asleep for a precise time period is impossible in Windows, nor can
you put a thread asleep for anything less than a couple of milliseconds.
A thread that calls Thread.Sleep with a value less than the thread quantum (10 - xx msec.)
simply gives up his thread quantum until scheduled again, which can be after a couple of
microseconds but also after a few seconds.
Except that 1/1000 is 0. Even if you changed it to 1.0/1000, the
parameter to Thread.Sleep is an int, not a double. The overload which
takes a TimeSpan ignores fractional milliseconds, according to the
documentation.
There is no way of telling a thread to sleep for a given number of
microseconds - Thread.SpinWait will wait for short times, but is almost
never useful.
Generally, if you need microsecond resolution, Windows (any flavour) is not
the right OS.
That being said, in old-fashioned Windows programming (no .NET or C#), I
have used a multimedia timer. The advantage is that the call-back function
is called the moment the timer triggers and not whenever Windows sees fit.
The disadvantage is that you need to assure data protection yourself, it is
probably only possible to use this in unmanaged code and you still get
millisecond resolution only.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.