public partial class Form1 : Form { public Form1() { InitializeComponent(); Dog dog = new Dog(); InsertDog(dog); dog.OnAlert(); //Console.WriteLine(); ...
public partial class Form1 : Form { public Form1() { InitializeComponent(); Dog dog = new Dog(); InsertDog(dog); dog.OnAlert(); //Console.WriteLine(); } public void InsertDog(Dog dog) { dog.alertHandler += new Dog.AlEventHandler(HostEventHandler); } public void HostEventHandler(object sender, EventArgs e) { Console.WriteLine("{0}\n",sender.ToString()); } } public class Dog { public delegate void AlEventHandler(object sender, EventArgs e); public event AlEventHandler alertHandler; protected int tm = 0; public void OnAlert() { if(this.alertHandler!=null) { while (true) { if (tm == 100) { this.alertHandler(this.tm, new EventArgs()); Console.WriteLine("2\n"); tm = 0; } tm++; Thread.Sleep(10); } } } }View Code