I have a code like this:

public class SomeClass
{
int _processProgress;
public int ProcessProgress
{
get { return _processProgress; }
set
{
_processProgress = value;
if (ProcessProgressChanged != null)
ProcessProgressChanged(value);
}
} public delegate void ProcessProgressChangedEventHandler(int progressPercentage);
public event ProcessProgressChangedEventHandler ProcessProgressChanged; public void ClearProcessProgressChangedEvent()
{
this.ProcessProgressChanged = null;
}
}

Will it unsubscribe all method in the ProcessProgressChanged event when I call the ClearProcessProgressChangedEvent() method?

My code is in C#, framework 4, build in VS2010 Pro, project is in Winforms.

Well, it'll effectively clear the list of subscribers, yes (by setting the underlying delegate field to null) - so that the next time ProcessProgress is set, no handlers will be called. It's not really setting theevent to null - it's setting the underlying field to null. It's just that the C# compiler is creating both an event (a subscribe/unsubscribe pair of methods) and a field (to store the handlers) using a single declaration.

You may find my article about events and delegates useful.

Note that your event-raising code currently isn't thread-safe. I don't know whether it needs to be or not, but you might want to consider using:

set
{
_processProgress = value;
var handlers = ProcessProgressChanged;
if (handlers != null)
{
handlers(value);
}
}

That way you won't get a NullReferenceException if the last handler is unsubscribed after the nullity check but before the invocation.

Setting an Event to Null的更多相关文章

  1. 错误信息:attempt to create saveOrUpdate event with null entity

    错误信息:attempt to create saveOrUpdate event with null entity; 这个错误网上答案比较多,我也不多说了. 我遇到的问题是在前台传过来的参数是nul ...

  2. 异常java.lang.IllegalArgumentException:attempt to create delete event with null entity

    异常java.lang.IllegalArgumentException:attempt to create delete event with null entity解决:路径问题,前台jsp和ja ...

  3. java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity

    今天想把ssh整合的代码跑起来,控制台就一直在报错,搞了半天!!! Hibernate: select computer0_.computerId as computer1_0_, computer0 ...

  4. attempt to create delete event with null entity

    解决办法:删除之前判断是否为空 if(Object != null){ session.delete(Object); }

  5. 操作MyBatis引发Error setting null for parameter #X with JdbcType OTHER .无效的列类型

    再用MyBatis操作Oracle的时候,传入null值而引发的错误 异常信息: org.springframework.jdbc.UncategorizedSQLException: Error s ...

  6. JS的event对象--知识点总结

    Event描述:event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. 需要注意的是:event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有 ...

  7. JS中的event 对象详解

    JS中的event 对象详解   JS的event对象 Event属性和方法:1. type:事件的类型,如onlick中的click:2. srcElement/target:事件源,就是发生事件的 ...

  8. Event/window.Event属性和方法

    type:事件的类型,如onlick中的click:srcElement/target:事件源,就是发生事件的元素:button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下 ...

  9. remove all event handlers from a control

    The sample code below will remove all Click events from button1 public partial class Form1 : Form { ...

随机推荐

  1. Interval Sum I && II

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  2. 新手学习爬虫之创建第一个完整的scrapy工程-糗事百科

    创建第一个scrapy工程-糗事百科 最近不少小伙伴儿,问我关于scrapy如何设置headers的问题,时间久了不怎么用,还真有的忘,全靠记忆去写了,为了方便大家参考,也方便我以后的查阅,这篇文章就 ...

  3. 用Python连接SQLServer抓取分析数据、监控 (pymssql)

    Python 环境:python3 服务器环境: centos6.5 数据库: Mysql 大概流程:在装有Python服务器,利用pymssql库连接MSSQL生产数据库取出数据然后写进mysql数 ...

  4. python与C交互中传入与读取内存空间

    使用用python调用c代码中,从外部传入一个固定大小的内存空间,这段内存需要是可写的 首先看下c中的函数 typedef struct ModelData { unsigned int model_ ...

  5. 我们在部署 HTTPS 网站时,该如何选择SSL证书?

    我们在部署 HTTPS 网站时,该如何选择SSL证书? 首次部署HTTPS网站的同学对选择什么样的SSL证书多多少少都有点迷茫. 这里考虑的因素确实不少:是否支持多域名.泛域名,价格,信息泄露的保额, ...

  6. SqlServer中 SET DATEFIRST更改

    在 SQL Server 中默认情况下,每周的开始都是从周日开始算起的,如果默认星期一呢? 这里有三种方式可以解决这个问题: 一:直接通过 SET DATEFIRST VALUE 来更改重新生成新的 ...

  7. 【算法】后缀自动机(SAM) 初探

    [自动机] 有限状态自动机的功能是识别字符串,自动机A能识别字符串S,就记为$A(S)$=true,否则$A(S)$=false. 自动机由$alpha$(字符集),$state$(状态集合),$in ...

  8. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  9. grep 详解

    grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.(global search regular expression(RE) and print out the l ...

  10. MIT-6.828-JOS-lab6:Network Driver

    MIT-6.828 Lab 6: Network Driver (default final project) tags: mit-6.828 os 概述 本lab是6.828默认的最后一个实验,围绕 ...