n System.ComponentModel, there's a class called CancelEventArgs which contains a Cancel member that can be set in event listeners. The documentation on MSDN explains how to use that to cancel events from within a listener, but how do I use it to implement my own cancelable events? Is there a way to check the Cancel member after each listener fires, or do I have to wait until after the event has fired all its listeners?

To check each listener in turn, you need to manually get the handlers via GetInvocationList:

class Foo
{
public event CancelEventHandler Bar; protected void OnBar()
{
bool cancel = false;
CancelEventHandler handler = Bar;
if (handler != null)
{
CancelEventArgs args = new CancelEventArgs(cancel);
foreach (CancelEventHandler tmp in handler.GetInvocationList())
{
tmp(this, args);
if (args.Cancel)
{
cancel = true;
break;
}
}
}
if(!cancel) { /* ... */ }
}
}

http://stackoverflow.com/questions/295254/how-do-i-implement-a-cancelable-event

How do I implement a cancelable event?的更多相关文章

  1. [转]关于event的两个常被忽略的api:isDefaultPrevented()和preventDefault()

    今天在robert penner(as3 singal的作者)的一篇blog文中顺藤摸瓜到了darron schall的另外一篇blog文(Creating Default, Cancelable E ...

  2. Weak Event Patterns

    https://msdn.microsoft.com/en-US/library/aa970850(v=vs.100).aspx In applications, it is possible tha ...

  3. (92)Wangdao.com_第二十五天_线程机制_H5 Web Workers 分线程任务_事件 Event

    浏览器内核 支撑浏览器运行的最核心的程序 IE 浏览器内核            Trident内核,也是俗称的IE内核Chrome 浏览器内核            统称为 Chromium 内核或 ...

  4. The .NET weak event pattern in C#

    Introduction As you may know event handlers are a common source of memory leaks caused by the persis ...

  5. Dojo实现Tabs页报错(三)

    用Dojo实现tab页的过程中,没有引用“on.js”,但是firebug调试时一直提示如下错误: on.js源码如下: define(["./has!dom-addeventlistene ...

  6. Month Scheme

    新的一个月,我要给自己立FLAG了, ABCDEFG HIJKLMN 天下事有难易乎,为之,则难者亦易矣,不为,则易者亦难矣. 这次采取的策略是,每完成一项work回来补充内容.希望能把这篇blog补 ...

  7. 百度前端技术学院2015JavaScript基础部分实现自己的小型jQuery

    // 实现一个简单的Query function $(selector) { ); if (firstChar == "#") { var len = selector.split ...

  8. Point Grey FlyCapture 实例汇总

    Example Language Description AsyncTriggerEx C++ Demonstrates some of the basic asynchronous trigger ...

  9. jQuery Questions:Front-end Developer Interview Questions

    Explain "chaining". Chaining allows us to run multiple jQuery methods (on the same element ...

随机推荐

  1. AMD规范基本结构

    AMD规范:使用 define 和 require ,基本结构如下: // 定义模块 define(['moduleA', 'moduleB', 'moduleC'], function (modul ...

  2. Dynamics CRM 2011 JScript

    if (!this.JSON) { this.JSON = {}; } (function () { function f(n) { return n < 10 ? '0' + n : n; } ...

  3. Dynamics CRM4.0 和 Dynamics CRM2011 Plugin 实现一样的功能的方法的比较

    1.给类型赋值不同 CRM4 plugin给lookup赋值为空 : Lookup lookupnull = new Lookup(); lookupnull.IsNull = true; looku ...

  4. MySql之on duplicate key update详解

    在我们的日常开发中,你是否遇到过这种情景:查看某条记录是否存在,不存在的话创建一条新记录,存在的话更新某些字段.你的处理方式是不是就是按照下面这样? $result = mysql_query('se ...

  5. html简介

    什么是 HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记语言 (ma ...

  6. [Hibernate] - one to many

    事实上one to many 和 many to one是一样的,这是一个相互的过程. hibernate.cfg.xml <?xml version="1.0" encod ...

  7. 基于SpringMVC下的Rest服务框架搭建【1、集成Swagger】

    基于SpringMVC下的Rest服务框架搭建[1.集成Swagger] 1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的re ...

  8. kafka_2.11-0.8.2.2的搭建

    一.下载官网的压缩包~ 修改conf/server.properties host.name=10.10.224.12  (修改为主机ip,不然服务器返回给客户端的是主机的hostname,客户端并不 ...

  9. set_union的几个例子

    获得两个集合的并集.两个输入序列须保证已排好序. 数组用的时候 // set_union example #include <iostream> #include <algorith ...

  10. ADO.NET(查询、属性扩展)

    一.ADO.NET 融合面向对象的查询语句 1.只查询一条数据 //数据访问中的select方法 public stu select(string xuehao) { stu s = null; cm ...