
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Get the Naming Rights for Your Events
|
1061
}
// Play the game.
Thread.Sleep(10000);
// Notify people.
Console.WriteLine("Patriots win the SuperBowl!");
// Signal one fan at a time.
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Notify fans");
ewhSuperBowl.Set( );
}
// Close the event.
ewhSuperBowl.Close( );
The AutoFan method is listed here:
public static void AutoFan( )
{
// Open the event by name.
EventWaitHandle ewhSuperBowl =
new EventWaitHandle(false,
EventResetMode.AutoReset,
@"Champs");
// Wait for the signal.
ewhSuperBowl.WaitOne( );
// Shout out.
Console.WriteLine("\"Yahoo!\" says {0}", Thread.CurrentThread.Name);
// Close the event.
ewhSuperBowl.Close( );
}
The output from the automatic event code will resemble the listing here (the AutoFan
threads might be in a different order):
Patriots win the SuperBowl!
Notify fans
"Yahoo!" says Fan 0
Notify fans
"Yahoo!" says Fan 2
Notify fans
"Yahoo!" says Fan 1
Discussion
EventWaitHandle is defined as deriving from WaitHandle, which in turn derives from
MarshalByRefObject. EventWaitHandle implements the IDisposable interface.
public class EventWaitHandle : WaitHandle
public abstract class WaitHandle : MarshalByRefObject, IDisposable
WaitHandle derives from MarshalByRefObject so you can use it across AppDomains,
and it implements
IDisposable to make ...