Timeout Pattern

Topics: Samples and Patterns
Coordinator
Jan 8, 2007 at 5:23 AM
static void TimerPortTest()
{
    Port<DateTime> timerPort = new Port<DateTime>();
    Port<string> somePort = new Port<string>();
    DispatchQueue dq = new DispatchQueue();
 
    // Run a selector on timeout or when "somePort" gets a message - which ever comes first.
    Selector.Activate(dq,
        Selector.Choice(
            Selector.SelectOne<string>(false, somePort,
                delegate(string s)
                {
                    Console.WriteLine("TimerPortTest: somePort got msg:{0}", s);
                }),
            Selector.SelectOne<DateTime>(false, timerPort,
                delegate(DateTime dt)
                {
                    Console.WriteLine("TimerPortTest: {0}", dt.ToString());
                })
        )
    );
    DispatchQueue.EnqueueTimer(TimeSpan.FromMilliseconds(50), timerPort);
    //somePort.Push("hello"); // Uncomment to get different result.
}