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);
            }
         }
    }

}

C#委托-事件示例的更多相关文章

  1. Observer设计模式中-委托事件-应用在消息在窗体上显示

    Observer设计模式:监视者模式.在类中的方法中处理的结果或者消息通过事件委托 的方式发送给主窗体. 因为在其它类中直接访问主窗体类,显示内容是不能直接调用控件赋值的,当然也有别的类似查阅控件名, ...

  2. python 全栈开发,Day55(jQuery的位置信息,JS的事件流的概念(重点),事件对象,jQuery的事件绑定和解绑,事件委托(事件代理))

    一.jQuery的位置信息 jQuery的位置信息跟JS的client系列.offset系列.scroll系列封装好的一些简便api. 一.宽度和高度 获取宽度 .width() 描述:为匹配的元素集 ...

  3. 理解jquery on 委托事件的机制

    前两天做了一个点击任意位置,都能关闭菜单的功能,因为菜单里面的每一个a,的点击事件都是用on绑定的.所以在阻止冒泡的时候不管用,今天特意来理解一下on的机制 on 是委托事件,利用的就是冒泡原理 $( ...

  4. .NET Core 跨平台物联网开发:设置委托事件(二)

    系列教程目录 (一) 连接阿里云IOT (二) 设置委托事件 (三) 上报属性 (四)  SDK文档 属性.方法.委托.类 http://pan.whuanle.cn/index.php?dir=up ...

  5. jQuery---jQ动画(普通,滑动,淡入淡出,自定义动画,停止动画),jQuery的事件,jQ事件的绑定/解绑,一次性事件,事件委托,事件冒泡,文档加载

    jQuery---jQ动画(普通,滑动,淡入淡出,自定义动画,停止动画),jQuery的事件,jQ事件的绑定/解绑,一次性事件,事件委托,事件冒泡,文档加载 一丶jQuery动画 show,hide, ...

  6. C# ~ 从 委托事件 到 观察者模式 - Observer

    委托和事件的部分基础知识可参见 C#/.NET 基础学习 之 [委托-事件] 部分: 参考 [1]. 初识事件 到 自定义事件: [2]. 从类型不安全的委托 到 类型安全的事件: [3]. 函数指针 ...

  7. C#委托,事件理解入门 (译稿)

    原文地址:http://www.codeproject.com/Articles/4773/Events-and-Delegates-Simplified 引用翻译地址:http://www.cnbl ...

  8. 关于ios使用jquery的on,委托事件失效

    $('.parents').on("click",'.child',function(){}); 类似上面这种,在ios上点击"child"元素不会起作用,解决 ...

  9. Asp.net用户控件和委托事件

    在Asp.net系统制作过程中,门户类型的网站,我们可以用DIV+CSS+JS+Ajax全部搞定,但是一旦遇到界面元素比较复杂的时候,还是UserControl比较方便一些,各种封装,各种处理,然后拖 ...

随机推荐

  1. jmeter csv 插件讲解

    1.变量名称 name,pwd 格式表示因为文本中分割默认是逗号所以变量设置也是按此格式如果想按其他格式可以在分隔符栏自定义 2.忽略首行: 有的csv读取你希望读取的数据有header如: user ...

  2. Spring Boot中Restful Api的异常统一处理

    我们在用Spring Boot去向前端提供Restful Api接口时,经常会遇到接口处理异常的情况,产生异常的可能原因是参数错误,空指针异常,SQL执行错误等等. 当发生这些异常时,Spring B ...

  3. 顾家办公两不误,容智ibot帮你实现高效居家办公

    春节假期结束,大部分企业已陆续开始复工.经调查显示,受新型冠状病毒疫情影响,不少企业开放了员工“在家办公“模式,就此,员工被动“SOHO”,在家办公火了. 2020 在家办公靠谱吗?会不会成为未来的趋 ...

  4. Java后台技术(Dubbo入门)

    我现在公司提供的产品是即时通讯软件,因为我从.net桌面应用开发转岗,从java后台转项目经理,让我有幸拥有了后台开发人员所有的权限,所有的后台源码和技术文档对我开放,可惜仅在后台待了3周不到,还没来 ...

  5. @ModelAttribute与@RequestBody的区别

    一.@ModelAttribute与@RequestBody的区别 @ModelAttribute与@RequestBody都是用来注解解析前端发来数据,并自动对应到所定义的字段名称. 这里先放结论, ...

  6. VS中使用C的一些函数报错的问题

    VS建议采用带_s的函数,如scanf_s.strcpy_s,但这些并不是标准C函数. 要想继续使用此函数,需要在源文件中添加以下指令就可以避免这个错误提示: #define _CRT_SECURE_ ...

  7. hash路由

    class HashRouter{ constructor(){ //用于存储不同hash值对应的回调函数 this.routers = {}; window.addEventListener('ha ...

  8. Hibernate学习(五)

    自关联测试案例 1.创建表 drop table if exists t_category ; create table t_category ( id ) primary key , name ) ...

  9. 吴裕雄 python 神经网络——TensorFlow训练神经网络:不使用指数衰减的学习率

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...

  10. html5或者移动端暴力定位城市-高德地图,可以取到当前的城市code,亲测好用

    复制 粘贴到html中打开!!!!! <!doctype html> <html> <head> <meta charset="utf-8" ...