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.
Asked
Active
Viewed 181 times
0
-
You could try `Thread.Sleep(0);`. It basically only suspends the thread if there is another thread of equal priority waiting. And if it is suspended it's immediately put into the wait queue. – juharr Jul 20 '19 at 11:26
-
I have already tried this and its the same issue, when set to 0 it runs up the CPU. This makes me wonder however - if set to zero and if I set its priority to 'lowest', does it matter if it takes 15% of my CPU? Because if the CPU starts getting up 100% it will start suspending this thread? – cem Jul 20 '19 at 11:48
-
Yeah, if you're not hitting 100% usage then honestly there's not much to worry about. – juharr Jul 20 '19 at 11:51
-
If this is about visualization then you must choose the refresh rate or update rate to be at least 24Hz for a fluent visual perception. This is the rate at which the human eye is processing images. This would be at least T = 1/f = 1/24Hz = 0,04166 seconds = 41,66 milliseconds. – BionicCode Jul 20 '19 at 14:00
-
What if instead of pushing the updates from the provider to the output, the output pulls the updates from the provider? Update an x/y value, and poll the values with the frequency you require them using a Timer. Your monitor can likely only refresh at 60fps, so there may be no point in updating faster than that. – MineR Jul 26 '19 at 03:16
1 Answers
1
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?

Stefan
- 17,448
- 11
- 60
- 79