Interrupt a sleeping Thread

后端 未结 7 1127
暗喜
暗喜 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:32

    Can't you just wrap your Thread.Sleep portion in a loop so that it loops 60 times then makes the other check? Of course all this does is trades off a simple if(somethingIndicatingQuit) versus the DateTIme if check, but assuming maybe you're real code does something more expensive, having a second loop could offer some minor CPU savings.

提交回复
热议问题