Is Thread.Sleep(Timeout.Infinite); more efficient than while(true){}?

前端 未结 6 870
Happy的楠姐
Happy的楠姐 2020-12-09 02:46

I have a console application that I would like to keep open all of the time while still listening in to events. I have tested Thread.Sleep(Timeout.Infinite); an

6条回答
  •  余生分开走
    2020-12-09 03:22

    Unlike while(true)..., Thread.Sleep does not use CPU cycles, so in this sense, the sleep is more efficient. In general, using Busy Waiting outside of spinlocks is strongly discouraged.

    If the thread is sleeping, is there anything that I should not be doing?

    Since your thread is blocked upon entry to Thread.Sleep, anything that you wish to do to its resources is a fair game.

提交回复
热议问题