监听

 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}
",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
");
                        tm = 0;
                    }
                    tm++;
                    Thread.Sleep(10);
                }
            }
        }

    }
View Code
原文地址:https://www.cnblogs.com/beenbynow/p/12201607.html