Setting an Event to Null
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的更多相关文章
- 错误信息:attempt to create saveOrUpdate event with null entity
错误信息:attempt to create saveOrUpdate event with null entity; 这个错误网上答案比较多,我也不多说了. 我遇到的问题是在前台传过来的参数是nul ...
- 异常java.lang.IllegalArgumentException:attempt to create delete event with null entity
异常java.lang.IllegalArgumentException:attempt to create delete event with null entity解决:路径问题,前台jsp和ja ...
- java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity
今天想把ssh整合的代码跑起来,控制台就一直在报错,搞了半天!!! Hibernate: select computer0_.computerId as computer1_0_, computer0 ...
- attempt to create delete event with null entity
解决办法:删除之前判断是否为空 if(Object != null){ session.delete(Object); }
- 操作MyBatis引发Error setting null for parameter #X with JdbcType OTHER .无效的列类型
再用MyBatis操作Oracle的时候,传入null值而引发的错误 异常信息: org.springframework.jdbc.UncategorizedSQLException: Error s ...
- JS的event对象--知识点总结
Event描述:event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. 需要注意的是:event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有 ...
- JS中的event 对象详解
JS中的event 对象详解 JS的event对象 Event属性和方法:1. type:事件的类型,如onlick中的click:2. srcElement/target:事件源,就是发生事件的 ...
- Event/window.Event属性和方法
type:事件的类型,如onlick中的click:srcElement/target:事件源,就是发生事件的元素:button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下 ...
- remove all event handlers from a control
The sample code below will remove all Click events from button1 public partial class Form1 : Form { ...
随机推荐
- The Smallest Difference
Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...
- linux调试工具glibc的演示分析-core dump double free【转】
转自:http://www.cnblogs.com/jiayy/p/3475544.html 偶然中发现,下面的两端代码表现不一样 void main(){ void* p1 = malloc(32) ...
- LTE:eMBMS架构
一个MBSFN区域是由一个或多个传输相同内容的小区组成的特殊区域.如图1所示,小区8和9都属于MBSFN区域C.一个MBSFN区域可由多个小区组成,一个小区也可以属于多个(至多8个,从36.331中的 ...
- 钉钉机器人-实现监控通知功能-python
1. 首先得创建有 一个 钉钉群.(因为只能发群通知) 2. 添加机器人,得到一个url: 3. 开始写Python脚本: # -*- coding: utf-8 -*- ""&q ...
- 开启@EnableRedisHttpSession
sessionId=569662ce-c6d5-42a9-a94b-c9df826df716 1800秒后失效 sessionId=23913542-9b5f-4699-8a87-1023b57f5f ...
- Hazelcast是什么
Hazelcast是什么 “分布式”.“集群服务”.“网格式内存数据”.“分布式缓存“.“弹性可伸缩服务”——这些牛逼闪闪的名词拿到哪都是ITer装逼的不二之选.在Javaer的世界,有这样一个 ...
- django 实现文件下载功能
一.概述 在实际的项目中很多时候需要用到下载功能,如导excel.pdf或者文件下载,当然你可以使用web服务自己搭建可以用于下载的资源服务器,如nginx,这里我们主要介绍django中的文件下载. ...
- linux内核内存分配(三、虚拟内存管理)
在分析虚拟内存管理前要先看下linux内核内存的具体分配我開始就是困在这个地方.对内核内存的分类不是非常清晰.我摘录当中的一段: 内核内存地址 ============================ ...
- 利用yum安装时,报错 [Errno 256] No more mirrors to try.
问题: [root@gg ~]# yum install -y perl-DBD-MySQL Loaded plugins: product-id, refresh-packagekit, secu ...
- 高能天气——团队Scrum冲刺阶段-Day 6
高能天气--团队Scrum冲刺阶段-Day 6 今日完成任务 于欣月:完善计步器功能:实现了历史步数统计和设置锻炼计划功能,并实现可视化图形界面 余坤澎:将闹钟部分和小游戏部分进行了合并 康皓越:配合 ...