Is it always bad to use Thread.Sleep()?

后端 未结 5 1141
天命终不由人
天命终不由人 2021-01-01 08:59

I created an extension method for the the class Random which executes an Action (void delegate) at random times:

public static clas         


        
5条回答
  •  迷失自我
    2021-01-01 09:54

    Is using Thread.Sleep bad? Generally not, if you really want to suspend the thread. But in this case you don't want to suspend the thread, you want to suspend the task.

    So in this case, you should use:

    await Task.Delay(minDuration);
    

    This will not suspend the entire thread, but just the single task you want to suspend. All other tasks on the same thread can continue running.

提交回复
热议问题