监听

 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
posted @ 2020-01-16 15:47  WbnProgress  阅读(238)  评论(0编辑  收藏  举报