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.

时间: 2024-08-30 04:33:03

Setting an Event to Null的相关文章

attempt to create delete event with null entity

SSH框架删除单条数据不会报错,一条以上数据回报: java.lang.IllegalArgumentException: attempt to create delete event with null entity 在网上查到到不少解决方法,但是都解决不了.自己调试把struts2封装的ids打印出来了,结果是--ID前面多了一个空格!!! Request URL: http://localhost:8080/manager/sysadmin/userAction_delete Form D

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

今天想把ssh整合的代码跑起来,控制台就一直在报错,搞了半天!!! Hibernate: select computer0_.computerId as computer1_0_, computer0_.computerName as computer2_0_, computer0_.computerPrice as computer3_0_, computer0_.computerType as computer4_0_ from computer computer0_java.lang.Il

【线程池】

参考资料 https://msdn.microsoft.com/en-us/library/windows/desktop/ms684841(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms686353(v=vs.85).aspx #include <windows.h> #include <tchar.h> #include <stdio.h> // // Thread

JS中的event 对象详解

JS中的event 对象详解 JS的event对象 Event属性和方法: 1. type:事件的类型,如onlick中的click: 2. srcElement/target:事件源,就是发生事件的元素: 3. button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下多个键,酒把这些值加起来,所以3就代表左右键同时按下:(firefox中 0代表左键,1代表中间键,2代表右键) 4. clientX/clientY:事件发生的时候,鼠标相对于浏览器窗口可视文档区域的

Mybatis:使用bean传值,当传入值为Null时,提示“无效的列类型”的解决办法

问题描述: 在使用mybatis对数据库执行更新操作时,parameterType为某个具体的bean,而bean中传入的参数为null时,抛出异常如下: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='pxh',

Javascript技术之详尽解析event对象

描述event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等.event对象只在事件发生的过程中才有效.event的某些属性只对特定的事件有意义.比如,fromElement 和 toElement 属性只对 onmouseover 和onmouseout 事件有意义. 例子下面的例子检查鼠标是否在链接上单击,并且,如果shift键被按下,就取消链接的跳转.<HTML><HEAD><TITLE>Cancels Links</TITLE

event跨进程通信

event天生的弱势,只有mutex可以感知丢失,就是将另一个进程关闭了,event无法感知. event1: #include <stdio.h> #include <stdlib.h> #include <Windows.h> char name[100] = "haihualovefang"; void main() { HANDLE event = CreateEventA(NULL, FALSE, FALSE, name); printf(

Event/window.Event属性和方法

type:事件的类型,如onlick中的click:srcElement/target:事件源,就是发生事件的元素:button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下多个键,酒把这些值加起来,所以3就代表左右键同时按下:(firefox中 0代表左键,1代表中间键,2代表右键)clientX/clientY: 事件发生的时候,鼠标相对于浏览器窗口可视文档区域的左上角的位置:(在DOM标准中,这两个属性值都不考虑文档的滚动情况,也就是说,无论文档滚动到哪 里,只要

event和window.event

type:事件的类型,如onlick中的click:srcElement/target:事件源,就是发生事件的元素:button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下多个键,酒把这些值加起来,所以3就代表左右键同时按下:(firefox中 0代表左键,1代表中间键,2代表右键)clientX/clientY:事件发生的时候,鼠标相对于浏览器窗口可视文档区域的左上角的位置:(在DOM标准中,这两个属性值都不考虑文档的滚动情况,也就是说,无论文档滚动到哪里,只要事件