Lately I've been working with graphics like "DrawImage" and it uses a lot of CPU resources. I would use Sleep(x milliseconds) to get the best affect when Drawing. But I noticed that when total CPU time was high, the value of my sleeps got messed up and were not what they should be. So I had an idea to find the right Sleep time. I made a function which uses performance counters to get the Total CPU Time. And based on what that value is, I return a sleep time that's takes into account how busy the system is. And it's actually working pretty well.

I just now made this code, so I'm sure it will need some tweaking or might even be just wrong in some cases, but the point of this is the idea. Not the actual code. Here's the function...

Code:
   Private Function CalculateSleep() As Int32
       Dim counter As Single = cpuUsage.NextValue
        Select Case counter
            Case > 30
                Return 0
            Case > 20
                If counter < 30 Then
                    Return 50
                Else
                    Return 0
                End If
            Case > 10
                If counter < 20 Then
                    Return 75
                Else
                    Return 25
                End If
            Case < 10
                Return 150
        End Select
    End Function
As I said, this code may be "out there" for now, but the point is the idea itself. I never thought about this before, but I think it can be useful.

Thanks for reading!