【读书笔记】事件Event
2010-03-23 17:52
288 查看
事件是一种允许类发送信号的方式,用来表明某个重要事情的发生。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Event { class Program { static void Main(string[] args) { LongTask It = new LongTask(); It.OnNotifyProgress += new LongTask.NotifyProgressDelegate(It_OnNotifyProgress); It.PerformTask(); Console.ReadLine(); } static void It_OnNotifyProgress(ProgressArgs pa) { Console.WriteLine("Progress on Long Task Complete. {0}% Complete.", pa.PercentComplete); } } class LongTask { public delegate void NotifyProgressDelegate(ProgressArgs pa); // Declare a delegate public event NotifyProgressDelegate OnNotifyProgress; // Create an event public void PerformTask() { for (int i = 0; i < 10000; i++) { //Perform some processing //... //notify subscribers that progress was made if (i % 100 == 0) { OnNotifyProgress(new ProgressArgs((int)i / 100)); } } } } class ProgressArgs { public int PercentComplete; public ProgressArgs(int pctComplete) { PercentComplete = pctComplete; } } }
事件概述
事件具有以下特点:发行者确定何时引发事件,订户确定执行何种操作来响应该事件。
一个事件可以有多个订户。一个订户可处理来自多个发行者的多个事件。
没有订户的事件永远不会被调用。
事件通常用于通知用户操作(如:图形用户界面中的按钮单击或菜单选择操作)。
如果一个事件有多个订户,当引发该事件时,会同步调用多个事件处理程序。要异步调用事件,请参见使用异步方式调用同步方法。
可以利用事件同步线程。
在 .NET Framework 类库中,事件是基于 EventHandler 委托和 EventArgs 基类的。
相关文章推荐
- Professional javascript For Web Developers 第2版读书笔记第5集event事件
- Professional javascript For Web Developers 第2版读书笔记第5集event事件2
- c# 关键字delegate、event(委托与事件)[MSDN原文摘录][2]
- 在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信
- 异步编程&事件循环event loops---总结
- Detect IFrame Load Event 探索Iframe的加载事件
- 实例 (用到 阻止事件冒泡 event.stopPropagation();)
- Javascript添加事件的addEventListener()及attachEvent()区别分析
- .net的event事件模型和java的interface事件模型
- onTouchEvent 和 onInterceptTouchEvent 事件触摸
- ie与ff下的event事件
- js 事件处理函数间的Event物件是否全等
- 跨平台的EVENT事件 windows linux
- Unity3D技巧在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信
- 安装事件过滤器(installEventFilter) ,过滤子控件事件,截获控件按键、鼠标事件
- 第五章 事件和数据回发机制 2009-04-02(读书笔记)
- android 事件处理机制之 requestDisallowInterceptTouchEvent
- EventFiringWebDriver网页事件监听(一)
- 《CLR via C#》读书笔记 之 事件
- 事件CEvent的使用