C# Winform program CPU occupies high reasons and solutions

Possible reasons for the high CPU usage of the program:

1. There is an infinite loop;

  • Why does the infinite loop lead to high CPU usage?
      Although the time-sharing operating system uses a time slice mechanism to manage the CPU time, that is to say, it will automatically switch from one process to the next at a certain time. However, after entering another process, if the process tells the system that it does not need to do anything now and does not need so much time, at this time, the system will switch to the next process. When the process is switched to the infinite loop, because It has been looping, always telling the system that it has something to do (essentially only in an infinite loop, not doing anything), then the system will try its best to save the time saved by other processes to make it do an infinite loop, and the CPU usage is not high That's weird.
  • Solution: Add "Thread.sleep(1);" in the infinite loop, as shown below:
while(true)
{
  if(进入条件)
    //该模块处理语句
  else
  {
    Thread.sleep(1);
}

2. High frequency IO operation.

PS: It is not finished, and will be added later.

Guess you like

Origin blog.csdn.net/youarenotme/article/details/80092327