using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace EventArgs
{
    //处理事件类
    class Server
    {
        public delegate void Handler(); //委托
        public event Handler Event;     //事件
        private Listener m_listener;
        public Server() 
        {
            m_listener = new Listener();
            this.Event += this.DealEvent;   //注册事件
            m_listener.Run();
        }        //处理方法
        private void DealEvent() 
        {
            if (null != Event)
            {
                Console.WriteLine("我在Server类的DealEvent方法中.");
            }
        }
    }    //触发事件类
    class Listener
    {
        //Server不是这个类的对象
        public Listener() 
        {        }        public void Run() 
        {
            this.OnHappen();
        }        //触发事件
        //有办法在这里触发事件吗?上面相应的要怎么改?
        public void OnHappen() 
        {
            Console.WriteLine("我要触发DealEvent.");
        }
    }    class Program
    {
        static void Main(string[] args)
        {
            Server server = new Server();
        }
    }
}
//目的就是想从Server里面启动Listener,然后当Listener有接收到连接时触发Server的方法体,谢谢大家费神了.. ...

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace EventArgs
    {
        //处理事件类
        class Server
        {
            private Listener m_listener;
            public Server()
            {
                m_listener = new Listener();
                m_listener.Happen += new EventHandler(m_listener_Happen);
                m_listener.Run();
            }        void m_listener_Happen(object sender, System.EventArgs e)
            {
                this.DealEvent();
            }        //处理方法
            private void DealEvent()
            {
                Console.WriteLine("我在Server类的DealEvent方法中.");
            }
        }    //触发事件类
        class Listener
        {
            public event EventHandler Happen;
            //Server不是这个类的对象
            public Listener()
            {        }        public void Run()
            {
                this.OnHappen();
            }        //触发事件
            //有办法在这里触发事件吗?上面相应的要怎么改?
            public void OnHappen()
            {
                Console.WriteLine("我要触发DealEvent.");
                this.Happen(this, null);
            }
        }    class Program
        {
            static void Main(string[] args)
            {
                Server server = new Server();
                Console.ReadKey();
            }
        }
    }
      

  2.   

    本帖最后由 caozhy 于 2012-02-25 00:14:00 编辑
      

  3.   

    楼主:
      你上面的是 委托(专业说法):
    你问如何触发的,这个我就简单说说吧:  以前学C 语言的时候,每个东东都有内存地址,并可以从地址访问。
    连function 也不例如,
       那么我们就可以用一变量 来存这个function 的地址, 就可以通过这变量 来运行这个function;
    现在用是C#,没有了指针了,但总要有替代的东东吧(函数指针的取代品啊) ----- 委托,
      用 变量来存函数地址用个好处,调用代码不变,只要改变量的值(内存address 就行了)
      这个,就是C# 委托啊!
      

  4.   

    楼主对事件、委托的概念还没搞清楚。事件是内部触发,由外部实现处理过程。
    public class ErrorEventArgs:EventArgs
    {
    private string error;
    public ErrorEventArgs(string error)
    {
    this.error = error;
    }
    public string Error
    {
    get return this.error;
    }
    }public delegate void ErrorEventHandler(object sender,ErrorEventArgs e);public class ErrorListener
    {
    public event ErrorEventHandler ErrorReceived;public void RaiseError()
    {
    string msg = "I raise a error.";
    if(this.ErrorReceived!=null)
    {
    this.ErrorReceived(this,new ErrorEventArgs(msg));
    }
    }
    }//在主界面UI实现
    public void UpdateError(object sender,ErrorEventArgs e)
    {
    this.lbl_msg.Text = e.Error;
    }