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. python3 web测试模块selenium

    selenium是一个用于web应用程序测试工具,selenium测试直接运行在浏览器中,就像真正的用户在操作一样,支持的浏览器包括IE(7,8,9,10,11),mozilla firefox,sa ...

  2. Jquery ajax json 不执行success的原因 坑爹

    最近在看jQuery的API文档,在使用到jQuery的ajax时,如果指定了dataType为json,老是不执行success回调,而是执行了error回调函数,极度郁闷.后面改为1.2.6版本可 ...

  3. MVC Form验证 登陆和退出Cookies的设定和消除

    红色部分为重点 1.webconfig配置  <system.web>节点下添加 <authentication mode="Forms"> <for ...

  4. C#中的GetElementsByClassName方法

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24     public static class Spread     {    ...

  5. C#上传图片(含有图片大小格式过滤以及改变像素安全存储)

    示例一: public JsonResult Upload(string parameter) { ]; try { //LogHelper.Info("文件长度:" + file ...

  6. File /data/binlog/mysql-bin.index' not found (Errcode: 13)

    [问题] 需要开启bin-log备份/恢复数据库,但是因为本身bin-log保存的位置存储太小,并且归类性也不好,所以自己新创建了/data/binlog来保存二进制日志 在/etc/my.cnf增加 ...

  7. java虚拟机规范(se8)——java虚拟机结构(四)

    2.7 对象的表示 java虚拟机并不要求对象满足任何特定的内部结构. 在Oracle的一些Java虚拟机实现中,对类实例的引用是指向句柄的指针,该句柄本身是一对指针:一个指向包含对象方法的表和指向表 ...

  8. python之Anaconda版本管理

    首先安装Anaconda,当其安装成功后,可以在cmd中测试是否安装成功,conda --version conda的环境管理 Conda的环境管理功能允许我们同时安装若干不同版本的Python,并能 ...

  9. NOIp 2018 普及组

    T1标题统计 传送门 题目描述 凯凯刚写了一篇美妙的作文,请问这篇作文的标题中有多少个字符? 注意:标题中可能包含大.小写英文字母.数字字符.空格和换行符.统计标题字 符数时,空格和换行符不计算在内. ...

  10. IdentityServer4结合AspNetCore.Identity实现登录认证踩坑填坑记录

    也可以自定义实现,不使用IdentityServer4.AspNetIdentity这个包,当然还要实现其他接口IResourceOwnerPasswordValidator. IProfileSer ...