CPU friendly infinite loop

前端 未结 11 1920
心在旅途
心在旅途 2020-12-04 09:15

Writing an infinite loop is simple:

while(true){
    //add whatever break condition here
}

But this will trash the CPU performance. This ex

11条回答
  •  爱一瞬间的悲伤
    2020-12-04 09:42

    You can use System.Threading.Timer Class which provides ability to execute callback asynchronously in a given period of time.

    public Timer(
        TimerCallback callback,
        Object state,
        int dueTime,
        int period
    )
    

    As alternative there is System.Timers.Timer class which exposes Elapsed Event which raises when a given period of time is elapsed.

提交回复
热议问题