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

后端 未结 3 2110
悲哀的现实
悲哀的现实 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:36

    I don't know if you can do that, but you can change the thread priority of the executing thread via the Priority property. You would set that by:

    Thread.CurrentThread.Priority = ThreadPriority.Lowest;
    

    Also, I don't think you really want to cap it. If the machine is otherwise idle, you'd like it to get busy on with the task, right? ThreadPriority helps communicate this to the scheduler.

提交回复
热议问题