How to restrict the CPU usage a C# program takes?

后端 未结 3 2111
悲哀的现实
悲哀的现实 2020-12-17 17:54

I am developing a C# program, and i have one function that consumes too much CPU. I would like to know a way to control this by code (not with any external application) and

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 18:43

    You can slow down a loop by calling Thread.Sleep(milliseconds) within the loop. That hands the CPU back to the scheduler.

    But 'consuming too much CPU' makes me think you might have more fundamental problems. Is this thread polling and waiting for something else? If so, you should consider the use of Events or some other kernel-based signalling mechanism.

提交回复
热议问题