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)
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.