How to limit CPU usage in a while loop
cpu-usage, limit, vb.net, while-loop
Solution
Use a timer event !!! Nearly no cpu effort.
Problem
How do you limit the CPU of a while loop? In this case, the code which is inside the while loop: ``` Private Sub wait(ByVal time) Dim sw As New Stopwatch sw.Start() Do While sw.ElapsedMilliseconds < time And StillOpen = True Application.DoEvents() Loop sw.Stop() End Sub ``` But now, here is the issue. This loop is allowing the while loop to run every second, once a second, and the `wait` sub is causing this delay, as it should. How can I limit the CPU that this is taking up? For some reason, my task manager says it is taking 50 CPUs to run this simple task, yet it should probably take no more than 1 or 2. Though the manager says it is taking that much CPU, my computer speed is not being affected at all, which is odd considering it is a two-year-old laptop. I don't want any users to freak out about it, but knowing how people are these days.... Anyway, the language is vb.net. Can someone please help me? Thanks! EDIT: To clarify, that code is not inside the while loop itself, but a call for the subroutine is, i.e. `wait(1000)`