using System; using System.Timers; using System.Threading; class MainClass { public static void TimerHandlerA(object obj, EventArgs e) { Console.WriteLine("Class A handler called"); } public static void TimerHandlerB(object obj, EventArgs e) { Console.WriteLine("Class B handler called"); } static void Main() { MyTimerClass mc = new MyTimerClass(); mc.Elapsed += TimerHandlerA; mc.Elapsed += TimerHandlerB; mc.Elapsed -= TimerHandlerB; Thread.Sleep(2000); } } public class MyTimerClass { public event EventHandler Elapsed; private void OnOneSecond(object obj, EventArgs e) { if (Elapsed != null) Elapsed(obj, e); } private System.Timers.Timer MyPrivateTimer = new System.Timers.Timer(); public MyTimerClass() { MyPrivateTimer.Elapsed += OnOneSecond; MyPrivateTimer.Interval = 1000; MyPrivateTimer.Enabled = true; } }
Class A handler called