user2005848
user2005848

Reputation: 406

Microsecond thread sleep

I need to give a PWM output through a special output on my machine. I was just wondering how I could achieve something similar to thread.sleep but with the resolution of microseconds?

Upvotes: 0

Views: 1706

Answers (1)

user2480047
user2480047

Reputation:

I am not sure why you want to rely on a so short period of time. You should bear in mind that the accuracy at these intervals is not too good and is even conditioned even by the CPU of the given machine. Thus, in any case you should thoroughly test the given approach under the given conditions to make sure that it delivers exactly what you want.

The smallest time value supported by .NET is a Tick (one hundred nanoseconds, as explained in this MSDN article) and thus you can build your own "sleep method" dealing with microseconds. Sample code:

 Dim count As Integer = 0
 Dim sw As Stopwatch = New Stopwatch
 Dim microSeconds As Integer = 5
 sw.Start()
 Do While (sw.ElapsedTicks < 10 * microSeconds)
 Loop
 sw.Stop()

Upvotes: 1

Related Questions