cem
cem

Reputation: 39

Stop a thread flooding the CPU without 'sleep'

I have a thread that sends x/y coordinates to some output. Without a Sleep(1) in there it hammers the CPU (mine at 13%) - but the output is extremely smooth. With a Sleep(1) in there the CPU usage drops to 1% but a slight stutter is perceived. I feel like I need a Sleep(0.5) or something. Something to just pull back the thread slightly from going at 100%. But sub Milliseconds sleeps do not seem possible.

Upvotes: 0

Views: 251

Answers (1)

Stefan
Stefan

Reputation: 17658

Your Thread.Sleep while have a maximum precision of about > 10 ms. So, if you want do have higher precision, you will need dedicated hardware. This can be a CPU, a GPU or even an audio clock.

If you use the CPU you'll see the high workload just to get the timing right. Depending on your needs you can pick one.

Here's also more info: Most accurate timer in .NET?

Upvotes: 1

Related Questions