How do I implement a cancelable event?
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?的更多相关文章
- [转]关于event的两个常被忽略的api:isDefaultPrevented()和preventDefault()
今天在robert penner(as3 singal的作者)的一篇blog文中顺藤摸瓜到了darron schall的另外一篇blog文(Creating Default, Cancelable E ...
- Weak Event Patterns
https://msdn.microsoft.com/en-US/library/aa970850(v=vs.100).aspx In applications, it is possible tha ...
- (92)Wangdao.com_第二十五天_线程机制_H5 Web Workers 分线程任务_事件 Event
浏览器内核 支撑浏览器运行的最核心的程序 IE 浏览器内核 Trident内核,也是俗称的IE内核Chrome 浏览器内核 统称为 Chromium 内核或 ...
- 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 ...
- Dojo实现Tabs页报错(三)
用Dojo实现tab页的过程中,没有引用“on.js”,但是firebug调试时一直提示如下错误: on.js源码如下: define(["./has!dom-addeventlistene ...
- Month Scheme
新的一个月,我要给自己立FLAG了, ABCDEFG HIJKLMN 天下事有难易乎,为之,则难者亦易矣,不为,则易者亦难矣. 这次采取的策略是,每完成一项work回来补充内容.希望能把这篇blog补 ...
- 百度前端技术学院2015JavaScript基础部分实现自己的小型jQuery
// 实现一个简单的Query function $(selector) { ); if (firstChar == "#") { var len = selector.split ...
- Point Grey FlyCapture 实例汇总
Example Language Description AsyncTriggerEx C++ Demonstrates some of the basic asynchronous trigger ...
- jQuery Questions:Front-end Developer Interview Questions
Explain "chaining". Chaining allows us to run multiple jQuery methods (on the same element ...
随机推荐
- windows下做react native官方例子遇到的问题
1.android/app/build.gradle文件中,指定了版本: compileSdkVersion 23buildToolsVersion "23.0.1" 需要在设置中 ...
- Redis学习手册(List数据类型)
一.概述: 在Redis中,List类型是按照插入顺序排序的字符串链表.和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right)添加新的元素.在插入时,如果该键并不存在,Redis ...
- BI案例:某通信企业综合报表和决策分析系统实施案例
业务需求 某家位列国内五大电信运营商之列的通信公司(以下简称“通信公司”)在多年的运营过程中,积累了大量的业务数据,分散在各个运营系统中(计费系统.九七工程.网管系统等).这些系统主要是面向公司的日常 ...
- smarty函数
内置函数(重要的几个): <{html_checkboxes name='nation' values=$code output=$name selected=$selid separator= ...
- 【python】浅谈for...else...语句
for循环可以和 else 子句同时使用(for...else).当迭代完for循环完整个列表失败后,会执行else语句.但循环中被 break 终止的情况下不会执行.如下例子所示: for i in ...
- js 标签云效果
下载:http://files.cnblogs.com/zjfree/js_tag_list.rar 效果如下: 源码如下: <html> <head> <meta ht ...
- Axure RP
Axure RP是一个专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写. Axure RP是美国Axu ...
- JavaScript-CheckBox全选/反选
//------------------------------------ // 全/反选 // param checkName checkbox的name属性 //---------------- ...
- 杀死future处理的阻塞线程
public class Test{ public static void main(String[] args){ ExecutorService pool = Executors.newFixed ...
- c++中ifstream读文件的问题(关于eof())
今天帮别人找BUG,是一段关于c++读写文件的问题,使用的是ifstream与outstream类,关于ofstream与ifstream的用法,此处不再獒述,见代码: #include<ios ...