namespace TestEvent
{
    class MyDeleChain
    {
        // 定义委托
        public delegate void MeDelegate();
        // 定义事件
        public event MeDelegate NotifyEveryOne;
        
        public void Notify()
        {
            // 如果事件不为 null
            while (NotifyEveryOne != null)                <-------------把if 改为了while
            {
                Console.WriteLine("触发事件:");
                // 触发事件
                NotifyEveryOne();
            }
        } 
    }
}
那么输出会无限的死循环,为什么?

解决方案 »

  1.   

    因为委托事件变量NotifyEveryOne 一直都不为空,所以一直循环,就编程死循环了
      

  2.   

    因为委托事件变量NotifyEveryOne 一直都不为空,所以一直循环,就编程死循环了
      

  3.   

    你的代码不会!但是会出现死循环!如果事件没注册就使用,就会死循环。示例如下namespace TestEvent
    {
        class MyDeleChain
        {
            // 定义委托
            public delegate void MeDelegate();
            // 定义事件
            public event MeDelegate NotifyEveryOne;
           
            public void Notify()
            {
                // 如果事件不为 null
                while (NotifyEveryOne != null)                <-------------把if 改为了while
                {
                    Console.WriteLine("触发事件:");
                    // 触发事件
                    NotifyEveryOne();
                }
            }        public void XX()
            {
                    ......
                    Notify();//触发事件
            }    }    class XXOO
        {
           public void XXCC()
           {
               MyDeleChain mdc = new MyDeleChain();           mdc.XX();//就是这了!!!由于没注册事件,死循环!       }
        }

      

  4.   

    修改一下 
    namespace TestEvent 

        class MyDeleChain 
        { 
            // 定义委托 
            public delegate void MeDelegate(); 
            // 定义事件 
            public event MeDelegate NotifyEveryOne; 
            bool IsEventOn=false;
            public void Notify() 
            {             // 如果事件不为 null 
                while (true)                <-------------把if 改为了while 
                { 
                   if(IsEventOn== true)
                  {
                    Console.WriteLine("触发事件:"); 
                    // 触发事件 
                    NotifyEveryOne(); 
                   IsEventOn==false;//复位 
                    }
                } 
            } 
    //做一个事件触发把 IsEventOn 改为ture
        } 

      

  5.   


    上面的那句判断:while(NotIfyEveryOne!=null)则输出“触发事件 ”  这里面的事件是什么啊~  不是就定义了一个事件吗?public event MeDelegate NotifyEveryOne; 什么是空事件啊   能解释一下不?