I find in both cases that the app still hogs around 97%
of the CPU but suprisingly the loop completion time
increases only slightly in either case (a fraction of a
second for 300,000 iterations)
Bill
Yes, I thought you were trying to avoid the "lock up" problem, where
other windows stop responding (or do so VERY slowly.)
If you want to decrease your CPU utilization, you'll have to put in
positive sleep values (try different values for the MOD value and the
Sleep(n) value).
The more you sleep, the less the CPU usage, and the longer it will
take for the loop to complete.
For k = 1 To 30000000
If k Mod 5000 = 0 Then
System.Threading.Thread.Sleep(1)
End If
Next k
The value of 5000 is arbitrary. The more "work" you're doing in your
loop, the lower the value you'll want to use. Otherwise, it will make
little or no difference at all.
Play around with the numbers and get a feel for what combinations do.
Doing a Sleep(1) means (ideally) a 1 millisecond sleep. So that means
you can only do that 1000 times a second.
Since the k Mod 5000 allows it to go every 5000 itterations, your loop
will have an overhead the following overhead:
30 million itterations
times
1 millisecond sleep / 5,000 itterations
= 6,000 milliseconds sleep
= 6 seconds
You have to ask yourself how long you're willing to let the loop take
-vs- how much CPU usage you want to let it use.
// CHRIS