WaitHandle
===========
包裝了為了共享某一資源而對排他通路進行等待的特定的作業系統對象.
該對象是典型地用作同步對象的基類. 從WaitHandle派生出來的類們定義了一個信号機制, 用于表示占有或釋放對于一個共享資源的而通路, 但是它們使用繼承而來的WaitHandle的方法來在需要等待通路共享資源的的時候阻塞自己.
WaitHandle.WaitOne方法, 作用是阻塞住目前線程, 直到目前的WaitHandle收到信号為止.
使用這個類的靜态方法來阻塞一個線程直到一個或多個synchronization對象收到了信号. 具體的代碼例子, 下面的原文出處中有.
ThreadPool.QueueUserWorkItem方法
================
将一個用于執行的方法添加到隊列中. 這個方法會線上程池中有可用的線程時被執行.
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx
WaitCallback代理
代表着被線程池中的線程執行的一個回調方法.
用法如下:
http://msdn.microsoft.com/en-us/library/system.threading.waitcallback.aspx
AutoResetEvent
===================
System..::.Object
System..::.MarshalByRefObject
System.Threading..::.WaitHandle
System.Threading..::.EventWaitHandle
System.Threading..::.AutoResetEvent
通知一個等待的線程, 告訴它一個事件已經發生了.
線程通過在AutoResetEvent上調用WaitOne方法來等待一個信号. 如果AutoResetEvent處于無信号狀态, 線程阻塞, 等待目前控制資源的線程來放出信号告訴它這個資源已經可用了, 放信号的方法是調用Set方法.
Calling Set signals AutoResetEvent to release a waiting thread. AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the non-signaled state. If no threads are waiting, the state remains signaled indefinitely.
If a thread calls WaitOne while the AutoResetEvent is in the signaled state, the thread does not block. The AutoResetEvent releases the thread immediately and returns to the non-signaled state.
EventWaitHandle的Set方法
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx
ManualResetEvent
繼承樹:
System..::.MarshalByRefObject
System.Threading..::.WaitHandle
System.Threading..::.EventWaitHandle
System.Threading..::.ManualResetEvent
該類的作用是, 通知一個或更多的線程: 一個事件發生了.
ManualresetEvent允許線程彼此使用信号方式進行通信. 典型地, 這個通信過程與這樣的一個任務有關: 該任務在某一個線程中必須先完成, 另一個線程才可以繼續.
當一個線程開始了一個那種它完成之後别的線程才可以繼續執行的動作時, 它調用Reset方法來讓ManualResetEvent進入到一個無信号(non-signaled)狀态. 這個線程可以被看作是在控制ManualResetEvent. 在ManualresetEvent上調用WaitOne的線程會被阻塞, 等待信号到來. 當控制者線程完成了動作, 它會調用Set方法來告訴等待的線程他們可以繼續了. 所有等待的線程就都被釋放掉了.
一旦他被信号通知了, ManualResetEvent仍然保持着signaled的狀态, 知道它被手動地reset. 也就是說, 調用WaitOne會迅速傳回.
You can control the initial state of a ManualResetEvent by passing a Boolean value to the constructor, true if the initial state is signaled and false otherwise.
ManualResetEvent can also be used with the static WaitAll and WaitAny methods.
練習
============
看懂下面的一段代碼吧.
The following code example demonstrates how to use wait handles to signal the completion of various stages of a complicated number calculation. The calculation is of the form: result = first term + second term + third term, where each term requires a precalculation and a final calculation using a calculated base number.