When
called, this method causes the thread to relinquish the rest of its time slice with the processor
and go to sleep. After the time has expired, the thread may be considered for scheduling again.
Naturally, the time duration you pass to Sleep() is reasonably accurate, but not exact. That??™s
because, at the end of the duration, the thread is not immediately given time on the processor.
There could be other higher-priority threads in the queue before it. Therefore, using Sleep()
to synchronize execution between two threads is strongly discouraged.
There is a special value, Timeout.Infinite, that you can pass to Sleep() to make the
thread go to sleep forever. You can wake a sleeping thread by interrupting it via the Thread.
Interrupt instance method. Interrupt() is similar to Abort() in that it wakes up the target
thread and throws a ThreadInterruptedException. Therefore, if your thread function is not
equipped to handle the exception, it will percolate all the way up the call stack until the runtime
ends the thread??™s execution. To be safe, you should make your call to Sleep() within a
Try block and catch the ThreadInterruptException. Unlike the ThreadAbortException, the
ThreadInterruptException is not automatically re-thrown by the runtime at the end of the
exception handler.
Pages:
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451