C# event
class Program
{
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater(60);
Cooler cooler = new Cooler(80); string temperature;
thermostat.OnTemperatureChange += heater.OnTemperatureChanged;
thermostat.OnTemperatureChange += cooler.OnTemperatureChanged;
Console.WriteLine("Enter temperature: ");
temperature = Console.ReadLine();
if(int.TryParse(temperature,out int result))
{
thermostat.CurrentTemperature = result;
}
Console.ReadLine();
}
} class Cooler
{
public float Temperature { get; set; }
public Cooler(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature>Temperature)
{
Console.WriteLine("Cooler:On");
}
else
{
Console.WriteLine("Cooler:Off");
}
}
} class Heater
{
public float Temperature { get; set; }
public Heater(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature<Temperature)
{
Console.WriteLine("Heater:On");
}
else
{
Console.WriteLine("Heater:Off");
}
}
} public class Thermostat
{
//Define the event publisher
public Action<float> OnTemperatureChange { get; set; } private float currentTemperatureValue;
public float CurrentTemperature
{
get
{
return currentTemperatureValue;
}
set
{
if(value!=currentTemperatureValue)
{
currentTemperatureValue = value;
OnTemperatureChange(value);
}
}
}
}
public class Thermostat
{
//Define the event publisher
public Action<float> OnTemperatureChange { get; set; } private float currentTemperatureValue;
public float CurrentTemperature
{
get
{
return currentTemperatureValue;
}
set
{
if(value!=currentTemperatureValue)
{
currentTemperatureValue = value;
OnTemperatureChange?.Invoke(value);
}
}
}
}
public class Thermostat
{
//Define the event publisher
public Action<float> OnTemperatureChange { get; set; } private float currentTemperatureValue;
public float CurrentTemperature
{
get
{
return currentTemperatureValue;
}
set
{
if(value!=currentTemperatureValue)
{
currentTemperatureValue = value;
Action<float> localOnChange = OnTemperatureChange;
if(localOnChange!=null)
{
localOnChange(value);
}
}
}
}
}
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater();
Cooler cooler = new Cooler(); Action<float> del1, del2, del3;
del1 = heater.OnTemperatureChanged;
del2 = cooler.OnTemperatureChanged;
Console.WriteLine("Invoke both delegates:");
del3 = del1;
del3 += del2;
del3(); Console.WriteLine("Invoke only del2:");
del3 -= del2;
del3();
Console.ReadLine();
}
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater();
Cooler cooler = new Cooler(); Action<float> del1, del2, del3;
del1 = heater.OnTemperatureChanged;
del2 = cooler.OnTemperatureChanged; Console.WriteLine("Combine delegates using + operator");
del3 = del1 + del2;
del3(); Console.WriteLine("UnCombine delegates using - operator!");
del3 = del3 - del2;
del3();
Console.ReadLine();
}
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater();
Cooler cooler = new Cooler(); thermostat.OnTemperatureChange += heater.OnTemperatureChanged; thermostat.OnTemperatureChange += (newTemperature) =>
{
throw new InvalidOperationException();
}; thermostat.OnTemperatureChange += cooler.OnTemperatureChanged;
Console.WriteLine("Enter temperature: ");
string temperature = Console.ReadLine();
if(int.TryParse(temperature,out int temp))
{
thermostat.CurrentTemperature = temp;
} Console.ReadLine();
}
private float currentTemperatureValue;
public float CurrentTemperature
{
get
{
return currentTemperatureValue;
}
set
{
if(value!=currentTemperatureValue)
{
currentTemperatureValue = value;
Action<float> localOnChange = OnTemperatureChange;
if(localOnChange!=null)
{
List<Exception> exceptionCollection = new List<Exception>();
foreach(Action<float> handler in OnTemperatureChange.GetInvocationList())
{
try
{
handler(value);
}
catch(Exception ex)
{
exceptionCollection.Add(ex);
}
} if(exceptionCollection.Count>)
{
throw new AggregateException("There were exceptions throw OnTemperatureChange Event subscribers.", exceptionCollection);
}
}
}
}
}
C# event的更多相关文章
- 如何利用ETW(Event Tracing for Windows)记录日志
ETW是Event Tracing for Windows的简称,它是Windows提供的原生的事件跟踪日志系统.由于采用内核(Kernel)层面的缓冲和日志记录机制,所以ETW提供了一种非常高效的事 ...
- [.NET] C# 知识回顾 - Event 事件
C# 知识回顾 - Event 事件 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6060297.html 序 昨天,通过<C# 知识回顾 - ...
- Atitit 解决Unhandled event loop exception错误的办法
Atitit 解决Unhandled event loop exception错误的办法 查看workspace/.metadata/.log org.eclipse.swt.SWTError: No ...
- Java模拟Windows的Event
场景 开发中遇到一个场景,业务操作会不定时的产生工作任务,这些工作任务需要放入到一个队列中,而另外会有一个线程一直检测这个队列,队列中有任务就从队列中取出并进行运算. 问题 业务场景倒是简单,只不过这 ...
- 事件EVENT与waitforsingleobject的使用
事件event与waitforsingleobject的配合使用,能够解决很多同步问题,也可以在数据达到某个状态时启动另一个线程的执行,如报警. event的几个函数: 1.CreateEvent和O ...
- 火狐浏览器中event不起作用解决办法--记录(一)
今天遇到了这个问题.IE,谷歌下都没问题,但在FF下却不起作用,很郁闷查了半天,看别人博文写了老长,结果试了要么起作用,但太麻烦,要么不起作用,说了那么多跟没说一样. 其实只要这一句代码就行:e=ar ...
- Event事件
妙味课堂-Event事件 1.焦点:当一个元素有焦点的时候,那么他就可以接受用户的输入(不是所有元素都能接受焦点) 给元素设置焦点的方式: 1.点击 2.tab 3.js 2.(例子:输入框提示文字) ...
- Event Sourcing Pattern 事件源模式
Use an append-only store to record the full series of events that describe actions taken on data in ...
- 严重: Exception sending context initialized event to listener instance of class
问题描述:Exception sending context initialized event to listener instance of class org.springframework.w ...
- [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)
原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...
随机推荐
- Python Django 支付宝 扫码支付
安装python-alipay-sdk pip install python-alipay-sdk --upgradepip install crypto 如果是python 2.7安装0.6.4这个 ...
- Java描述设计模式(08):桥接模式
本文源码:GitHub·点这里 || GitEE·点这里 一.桥接模式简介 1.基础描述 桥梁模式是对象的结构模式.又称为柄体(Handle and Body)模式或接口(Interface)模式.桥 ...
- Python 一键获取百度网盘提取码
该 GIF 图来自于官网,文末有给出链接. 描述 依托于百度网盘巨大的的云存储空间,绝大数人会习惯性的将一些资料什么的存储到上面,但是有的私密链接需要提取码,但是让每个想下载私密资源的人记住每一个提取 ...
- 【带着canvas去流浪(9)】粒子动画
目录 一. 粒子特效 二. 开发中遇到的问题 2.1 卡顿 2.2 轨迹 2.3 复位 2.4 防护层 2.5 二维向量类 三. 实现讲解 3.1 粒子类的update方法 3.2 粒子群的绘制 3. ...
- 并发容器之ConcurrentHashMap(JDK 1.8版本)
1.ConcurrentHashmap简介 在使用HashMap时在多线程情况下扩容会出现CPU接近100%的情况,因为hashmap并不是线程安全的,通常我们可以使用在java体系中古老的hasht ...
- Customize the Application UI and Behavior 自定义应用程序UI和行为
In XAF, the business model defines the database structure and UI appearance. Changes to your persist ...
- es6 之class介绍
class ECMAScript 2015 中引入的 JavaScript 类实质上是 JavaScript 现有的基于原型的继承的语法糖.类语法不会为JavaScript引入新的面向对象的继承模型. ...
- 【Gradle】Groovy基础
Groovy基础 Groovy是基于JVM虚拟机的一种动态语言.每个Gradle的build脚本文件都是一个Groovy脚本文件. 字符串 在Groovy中,分号不是必需的.在Groovy中,单引号和 ...
- Thymeleaf常用语法:表达式语法之运算符
Thymeleaf表达式语法之常量分为字符串常量.数字常量.布尔值常量.空值常量:运算符分为算术运算符.关系运算符.条件运算符.无操作符. 开发环境:IntelliJ IDEA 2019.2.2Spr ...
- BIM工程信息管理新系统- 系统管理模块
系统管理模块 1.实体类 public partial class T_Role { public string RoleId { get; set; } public string RoleName ...