Program.cs

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

namespace Event_Delegate
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A(); //定义首领A
            B b = new B(a);

a.Raise("左");
            Console.ReadLine();
            //====
            var test = new Heater();
            test.Onboiled += TestOnboiled;
            test.Onboiled += TestOnboiled;
            test.Begin();
            Console.ReadKey();
        }
        static void TestOnboiled(object sender, EventArgs e)
        {
            Console.WriteLine("Hello 事件被调用");

}
    }

public delegate void RaiseEventHandler(string hand);

/// <summary>
    /// 首领A
    /// </summary>
    public class A
    {
        /// <summary>
        /// 首领A举杯事件
        /// </summary>
        public event RaiseEventHandler RaiseEvent;

public void Raise(string hand)
        {
            Console.WriteLine("首领A举杯");

//调用举杯事件,传入左或右手作为参数
            if (RaiseEvent != null)
            {
                RaiseEvent(hand);
            }
        }
    }
    /// <summary>
    /// 部下B
    /// </summary>
    public class B
    {
        A a;
        public B(A a)
        {
            this.a = a;
            a.RaiseEvent += new RaiseEventHandler(a_raiseEvent);
        }
        void a_raiseEvent(string hand)
        {
            if (hand.Equals("左"))
            {
                Console.WriteLine("部下B发起攻击 ");
            }
        }
    }
}
Client_1.cs:

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

using System.Threading;

namespace Event_Delegate
{
    class Client_1
    {
    }

public class Heater
    {
        public event EventHandler Onboiled;
        private void RasieBoiledEvent()
        {
            if (Onboiled == null)
            {
                Console.WriteLine("加热完成,处理订阅事件为空");
            }
            else
            {
                Onboiled(this,new EventArgs());
            }
        }
        private Thread heatThread;
        public void Begin()
        {
            heatTime = 5;
            heatThread = new Thread(new ThreadStart(Heat));
            heatThread.Start();
            Console.WriteLine("加热器已经开启",heatTime);
        }
        private int heatTime;
        private void Heat()
        {
            while (true)
            {
                Console.WriteLine("加热还需要{0} 秒",heatTime);
                if (heatTime == 0)
                {
                    RasieBoiledEvent();
                    return;
                }
                heatTime--;
                Thread.Sleep(1000);
            }
         }
    }

}

最新文章

  1. 图像处理中任意核卷积(matlab中conv2函数)的快速实现。
  2. 设置Android Studio启动时可选最近打开过的工程
  3. maven 错误: 程序包org.junit不存在
  4. Spring.Net Remoting 相关使用
  5. PC缺少一个或多个网络协议 qq可登录(win10)
  6. sed命令的基本使用
  7. Lotus开发性能优化
  8. C#关闭word进程
  9. 在RHEL5.2 64bit下编译安装log4cxx错误
  10. mysql 事件调度器
  11. leetcode 239 Sliding Window Maximum
  12. Xcode 断点调试,取消直接进入到汇编语言界面处理
  13. c# WMI获取机器硬件信息(硬盘,cpu,内存等)
  14. C++安装失败解决办法
  15. 经历:Java中字符串中按照多个字符拆分或替换:split()和replaceAll()
  16. Git命令详解(一)-个人使用
  17. Oracle视图基础
  18. LCS小结(O(∩_∩)O~吽吽)
  19. &lt;a href=&quot;#&quot; onclick=&quot;history.back();&quot;&gt;&lt;/a&gt;这样写为什么是对的? -(转)
  20. USACO刷题之路

热门文章

  1. hadoop学习笔记(二):hdfs优点和缺点
  2. 【MYSQL建库和建表】MYSQL建库和建表
  3. 健壮的I/O(RIO)
  4. JS--easyui通用验证
  5. 探讨 Git 代码托管平台的若干问题
  6. MS17_010漏洞攻击Windows7
  7. 微权获取openid信授
  8. MariaDB-Galera部署
  9. Android 短信模块分析(七) MMS数据库定义及结构整理
  10. C++11常用特性介绍——decltype关键字