Save CPU Utilization In An Empty Loop

Sometimes you need to kill some time by running an empty for or while loop. The only problem is that the CPU utilization will soon reach 100% for a single core CPU or 50% for a duel-core. Oh, my Goodness, it is horrible.

How to solve the problem and protect your/and your users’ CPU? A simple walk around is there. Just write Thread.Sleep(??); inside the loop. In my case, I let it sleep for 500-ms. In this way, the utilization can be reduced to a quite small number.

© Lex Li. All rights reserved. The code included is licensed under CC BY 4.0 unless otherwise noted.

© - Lex Li. All rights reserved.

Using the Chirpy theme for Jekyll.

Last updated on May 01, 2024