C#事件與委託

示例一:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace EventAndDelegate
{
    class Program
    {
        static void Main(string[] args)
        {
            Server server = new Server();
            //訂閱成功啓動的事件
            server.Started += new Server.ServerEventHandler(server_Stopped);
            //訂閱成功停止的事件
            server.Stopped += new Server.ServerEventHandler(server_Started);
            //啓動服務
            server.Start();
            Thread.Sleep(3000);
            server.Stop();
        }
        static void server_Stopped(object sender, ServerEventArgs e)
        {
            Console.WriteLine("Server successfully stopped at: {0}", e.FireDateTime);
        }
        static void server_Started(object sender, ServerEventArgs e)
        {
            Console.WriteLine("Server successfully started at: {0}", e.FireDateTime);
        } 
    }

    //事件處理函數參數類
    public class ServerEventArgs : System.EventArgs
    {
        public ServerEventArgs()
        {
            this.FireDateTime = DateTime.Now;
        }
        public ServerEventArgs(DateTime fireDateTime)
        {
            this.FireDateTime = fireDateTime;
        }

        private DateTime fireDateTime;
        public DateTime FireDateTime
        {
            get
            {
                return fireDateTime;
            }
            set
            {
                fireDateTime = value;
            }
        }
    }

    public class Server
    {
        public delegate void ServerEventHandler(object sender, ServerEventArgs e);

        public event ServerEventHandler Started;
        public event ServerEventHandler Stopped;

        protected virtual void DoStataed(object sender, ServerEventArgs e)
        {
            if (Started != null)
            {
                Started(sender,e);
            }
        }
        protected virtual void DoStopped(object sender, ServerEventArgs e)
        {
            if (Stopped != null)
            {
                Stopped(sender,e);
            }
        }
        
        /// <summary>
        /// 執行服務器的啓動操作
        /// </summary>
        public void Start()
        {
            //啓動服務
            DoStataed(this, new ServerEventArgs(DateTime.Now));
        }
        /// <summary>
        /// 執行服務器的停止操作
        /// </summary>
        public void Stop()
        {
            //在此停止服務器
            DoStopped(this,new ServerEventArgs(DateTime.Now));
        }
    }
}


 

示例二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public delegate string GetName();

    class Program
    {
        static void Main(string[] args)
        {
            Heater heater = new Heater();
            heater.BoilEvent += new Alarm().MakeAlert;//註冊報警器事件
            heater.BoilEvent += Display.ShowMsg;//註冊顯示水溫事件
            heater.BoilWater();//開始燒水
            Console.ReadLine();
        }
    }
    public class Heater
    {
        public delegate void BoilHandler(int param);
        public event BoilHandler BoilEvent;
        private int temperature;//水溫
        /// <summary>
        /// 燒水
        /// </summary>
        public void BoilWater()
        {
            for (int i = 0; i < 100; i++)
            {
                temperature = i;
                if (temperature <= 95)
                    Console.WriteLine("溫度:{0}", temperature);
                if (temperature > 95)
                {
                    if (BoilEvent != null)
                        BoilEvent(temperature);
                }
            }
        }
    }
    //報警器
    public class Alarm
    {
        public void MakeAlert(int param)
        {
            Console.WriteLine("Alarm:嘀嘀嘀,水已經 {0} 度了:", param);
        }
    }
    //顯示水溫
    public class Display
    {
        public static void ShowMsg(int param)
        {
            Console.WriteLine("Display:水已燒開,當前溫度:{0}度。", param);
        }
    }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章