欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

监听

程序员文章站 2023-09-28 17:39:45
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);
                }
            }
        }

    }