thread get 100% CPU very fast

后端 未结 5 1282
轮回少年
轮回少年 2021-01-18 07:44

I am implementing a very basic thread in C#:

private Thread listenThread;

public void startParser()
{
   this.listenThread = new Thread(new ThreadStart(chec         


        
5条回答
  •  感动是毒
    2021-01-18 08:01

    while (true) is what killing your CPU.

    You can add Thread.Sleep(X) to you while to give CPU some rest before checking again.

    Also, seems like you actually need a Timer.

    Look at one of the Timer classes here http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx.

    Use Timer with as high pulling interval as you can afford, 1 sec, half a sec.
    You need to tradeoff between CPU usage and the maximum delay you can afford between checks.

提交回复
热议问题