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. kettle常见问题解决

    开源ETL工具kettle系列之常见问题 摘要:本文主要介绍使用kettle设计一些ETL任务时一些常见问题,这些问题大部分都不在官方FAQ上,你可以在kettle的论坛上找到一些问题的答案 1. J ...

  2. HTTP响应头和请求头信息对照表

    HTTP请求头提供了关于请求,响应或者其他的发送实体的信息.HTTP的头信息包括通用头.请求头.响应头和实体头四个部分.每个头域由一个域名,冒号(:)和域值三部分组成. 通用头标:即可用于请求,也可用 ...

  3. ORA-27101:shared memory realm does not exist的问题

    ORA-27101:shared memory realm does not exist的问题 登陆SQLPlus时出现: ORA-01034:ORACLE not avaiable ORA-2710 ...

  4. android 工程出现感叹号错误

    android 工程出现感叹号错误: 错误问题分析,曾经导入的jar已经不存在工程目录中,project从其他地方导入时.没有及时更新,比如说svn下载到.project的文件,或者是path的文件. ...

  5. C# Redis实战(二) [转]

    二.Redis服务  在C# Redis实战(一)中我将所有文件拷贝到了D盘redis文件夹下,其中redis-server.exe即为其服务端程序,双击即开始运行,如图               ...

  6. [web] 200 OK (from cache) 与 304 Not Modified

    为什么有的缓存是 200 OK (from cache),有的缓存是 304 Not Modified 呢?很简单,看运维是否移除了 Entity Tag.移除了,就总是 200 OK (from c ...

  7. 对自己的文件使用keystore签名

    keytool 对jar包签名步骤: 1.将程序打成jar包. 2.生成keystore数字证书keytool -genkey -keystore xxx.keystore -alias xxx -v ...

  8. 用Opera Mobile调试手机版网页【转】

    注意:新版本的opera已经采用webkit内核,没有dragonfly了. 要下载12版的http://get.geo.opera.com/pub/opera/win/1216/int/Opera_ ...

  9. CSS设置DIV背景色渐变显示

    本文转载自:http://blog.csdn.net/gingerredjade/article/details/12191741 <style type="text/css" ...

  10. iPerf - The network bandwidth measurement tool

    What is iPerf / iPerf3 ? iPerf3 is a tool for active measurements of the maximum achievable bandwidt ...