WaitAny Pattern

Topics: Samples and Patterns
Coordinator
Jan 8, 2007 at 4:25 AM
public static void WaitAny()
{
    // Block until we get an object on any port. Remove the event logic for non-blocking async waitany.
    Port<string> p0 = new Port<string>();
    Port<string> p1 = new Port<string>();
    AutoResetEvent ar = new AutoResetEvent(false);
    DispatchQueue dq = new DispatchQueue();
    SendErrorsToConsole(dq);
 
    Selector.Activate(dq,
        Selector.SelectOne(false, new Port<string>[] {p0,p1},
            delegate(string s)
            {
                Console.WriteLine("WaitAny: " + s);
                ar.Set();   // Signal blocked thread.
            })
    );
 
    p1.Push("one");     // Some code pushes a value on one of the ports.
    p0.Push("two");     // Will stay in port as selector not persistant.
    ar.WaitOne();       // Block current thread until signaled.
}