C#中使用事件(代码示例)

/**********************************************************************************************
 * Date:2008-01-24
 * Function:Exercise for using event in .Net
 * Author:Carlo Zhai
 * Note:
 * C#中使用事件需要的步骤
    1.创建一个委托
    2.将创建的委托与特定事件关联
    3.编写事件处理程序
    4.利用编写的事件处理程序生成一个委托实例
    5.把这个委托实例添加到产生事件对象的事件列表中去,这个过程又叫订阅事件
 * *********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace EventUsing_SimpleDemo
{
    public delegate void MessageHandler(string messageTxt);//1.创建一个委托 这里必须是多播委托(void)
    public class Connection
    {
        private delegate void InternalEventHandler(string messageTxt);
        private event InternalEventHandler InternalEventArrived;
        public event MessageHandler MessageArrived;//2.将创建的委托与特定事件关联,public private 等修饰符都可以使用
        private Timer pollTimer;
        public Connection()
        {
            pollTimer = new Timer(100);//100milliseconds
            pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
            InternalEventArrived += new InternalEventHandler(Connection_InternalEventArrived);
        }

     
        public void Connect()
        {
            pollTimer.Start();
        }
        public void DisConnect()
        {
            pollTimer.Stop();
        }
        void CheckForMessage(object sender, ElapsedEventArgs e)
        {
            Random random = new Random();
            if ((random.Next(0, 9) == 5) && (MessageArrived != null))//MessageArrived!=null这个非常重要,容易遗忘
            {
                MessageArrived(string.Format("Hello,the time is {0}", DateTime.Now.ToString()));//触发事件
            }
            if ((random.Next(0, 9) == 8) && (InternalEventArrived != null))//MessageArrived!=null这个非常重要,容易遗忘
            {
                InternalEventArrived(string.Format("The Internal Event Arrived at {0}", DateTime.Now.ToString()));//触发事件
            }
        }
        void Connection_InternalEventArrived(string messageTxt)
        {
            Console.WriteLine(messageTxt);
        }
    }

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Connection myConnection = new Connection();
            //下面这两句注释掉再对比
            myConnection.MessageArrived += new MessageHandler(Display.DisplayMsg);//5.把这个委托实例添加到产生事件对象的事件列表中去,这个过程又叫订阅事件
            myConnection.MessageArrived += new MessageHandler(myConnection_MessageArrived);
            myConnection.Connect();
            Console.WriteLine("Please input - to remove the event handle  myConnection_MessageArrived;/n input + to add the event handle  myConnection_MessageArrived;/n input Enter to quit;");
            while (true)
            {
                string inStr = Console.ReadLine();
                if (inStr.Equals("-"))
                    myConnection.MessageArrived -= myConnection_MessageArrived;
                if (inStr.Equals("+"))
                    myConnection.MessageArrived += new MessageHandler(myConnection_MessageArrived);
                if (inStr == "")
                    return;
            }
      

 

        }

        static void myConnection_MessageArrived(string messageTxt)
        {
            Console.WriteLine("The method or operation is not implemented.");
        }


    }

    public class Display
    {
        //编写事件处理程序
        public static void DisplayMsg(string displayMsg)
        {
            Console.WriteLine(string.Format("MessageArrived{0}", displayMsg));
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值