Interrupt a sleeping Thread

后端 未结 7 1067
暗喜
暗喜 2020-12-13 16:08

Is there a way to Interupt a sleeping thread? If I have code similar to this.

while(true){
    if(DateTime.Now.Subtract(_lastExecuteTime).TotalHours > 1)         


        
7条回答
  •  时光说笑
    2020-12-13 16:15

    You can use Thead.Interrupt to wake a sleeping thread. It will cause a ThreadInteruptedException on the blocked thread, so it's not the most elegant or efficient approach.

    You also need to be weary that interrupting a thread in this way is unsafe. It does not provide control over the point at which the thread is aborted, and therefore should not be used without careful consideration as to the consequences. As mentioned in other answers already, it's is far better to use signal based techniques to control when the thread terminates in a controlled manner.

提交回复
热议问题