In wince or Windows Moblie development, a large amount of batch work is often submitted to worker Thread, and the interface process (UI thread) needs to be synchronized when the batch work completes, or if the process fails and the exception occurs. (PS: Sometimes two worker thread is synchronized, not UI thread, which is determined by the specific application.) It is often necessary to wait for the event. The wait events provided by the NET Framework are encapsulated inside the System.Threading.WaitHandle. But. The WaitHandle under the NET Compact Framework are not available. NET Framework, which provides only the WaitOne function (waiting for an event) in the waiting time. In fact, in general application, the UI process often waits for an event to suffice, below demonstrates the use of WaitOne.
Since WaitHandle is an abstract class (abstract class), the example uses its sub-class AutoResetEvent.
Defines field, both parent and child threads need access to the
private static AutoResetEvent autoEvent = new AutoResetEvent (false);
public bool Connect()
{
//Do sth. eg make connections.
ThreadPool.QueueUserWorkItem(
new WaitCallback(CheckConnection), null);
// Wait for work method to signal.
if (autoEvent.WaitOne(5000, false))
{
return true;
}
else
{
return false;
}
}
private void CheckConnection(Object stateInfo)
{
while(true)
{
if (CheckConnection())
{
// Signal that work is finished.
autoEvent.Set();
}
}
}