static void Main(string[] args) { Timer timer = new Timer(); timer.Interval = 1000; Boy boy = new Boy(); Girl girl = new Girl(); timer.Elapsed += boy.Action; timer.Elapsed += girl.Action; timer.Start(); Console.ReadLine(); }
static void Main(string[] args) { Timer timer = new Timer();//事件的拥有者 timer.Interval = 1000; Boy boy = new Boy();//事件的响应者 Girl girl = new Girl(); timer.Elapsed += boy.Action;//事件的成员,+=是事件订阅的操作符 timer.Elapsed += girl.Action; timer.Start(); Console.ReadLine(); }
public static void DoExample() { System.Timers.Timer timer = new System.Timers.Timer(); //event source => timer timer.Interval = 1000; Boy boy = new Boy(); //event subscriber class => boy Girl girl = new Girl(); //兩個物件訂閱同一個事件 timer.Elapsed += boy.Action; //event => timer.Elapsed timer.Elapsed += girl.Action; //+= => subscribe , event handler => girl.Action timer.Elapsed += new ElapsedEventHandler(boy.Action); //早期做法 timer.Start(); Console.ReadKey(); }