public class Publisher { public void Print() { Console.WriteLine("来活了"); } }
class Subscriber { public delegate void NumManipulationHandler(); public event NumManipulationHandler DoJobEvent; public void StatusChanged() { if (DoJobEvent != null) { DoJobEvent(); Console.WriteLine("唉,又要干活了。。。"); Console.ReadLine(); } else { Console.WriteLine("还没来活呀。。。"); Console.ReadKey(); } } }
public class Program { static void Main(string[] args) { Publisher publisher = new Publisher(); Subscriber subscriber = new Subscriber(); subscriber.DoJobEvent += publisher.Print; subscriber.StatusChanged(); } }