Why is Thread.Sleep so harmful

前端 未结 8 2039
天涯浪人
天涯浪人 2020-11-22 06:30

I often see it mentioned that Thread.Sleep(); should not be used, but I can\'t understand why this is so. If Thread.Sleep(); can cause trouble, are

8条回答
  •  鱼传尺愫
    2020-11-22 06:36

    SCENARIO 1 - wait for async task completion: I agree that WaitHandle/Auto|ManualResetEvent should be used in scenario where a thread is waiting for task on another thread to complete.

    SCENARIO 2 - timing while loop: However, as a crude timing mechanism (while+Thread.Sleep) is perfectly fine for 99% of applications which does NOT require knowing exactly when the blocked Thread should "wake up*. The argument that it takes 200k cycles to create the thread is also invalid - the timing loop thread needs be created anyway and 200k cycles is just another big number (tell me how many cycles to open a file/socket/db calls?).

    So if while+Thread.Sleep works, why complicate things? Only syntax lawyers would, be practical!

提交回复
热议问题