Anatomy on the event add/remove handler

If you open a C# class, and if the C# class has event delcared, then you might be able to see code as follow. 

    // Events
    internal event EventHandler<ModuleInitializeArgs> AfterModuleInitialized
    {
        add
        {
            EventHandler<ModuleInitializeArgs> handler2;
            EventHandler<ModuleInitializeArgs> afterModuleInitialized = this.AfterModuleInitialized;
            do
            {
                handler2 = afterModuleInitialized;
                EventHandler<ModuleInitializeArgs> handler3 = (EventHandler<ModuleInitializeArgs>) Delegate.Combine(handler2, value);
                afterModuleInitialized = Interlocked.CompareExchange<EventHandler<ModuleInitializeArgs>>(ref this.AfterModuleInitialized, handler3, handler2);
            }
            while (afterModuleInitialized != handler2);
        }
        remove
        {
            EventHandler<ModuleInitializeArgs> handler2;
            EventHandler<ModuleInitializeArgs> afterModuleInitialized = this.AfterModuleInitialized;
            do
            {
                handler2 = afterModuleInitialized;
                EventHandler<ModuleInitializeArgs> handler3 = (EventHandler<ModuleInitializeArgs>) Delegate.Remove(handler2, value);
                afterModuleInitialized = Interlocked.CompareExchange<EventHandler<ModuleInitializeArgs>>(ref this.AfterModuleInitialized, handler3, handler2);
            }
            while (afterModuleInitialized != handler2);
        }
    }

 

You may wonder why all the hassle when you you just wantted to comine a handler to an event field. 

but let's see why this??

in multple situation, it is common that you lost update, where event are immutable, when you combine a event with a event handler, then a new event will be returned, so before you switch the combined result with the event field, if there is another thread kicks in and stole the update, where it has updated the AfterModuleInitialized event , and then the forestalled thread resume, and it is highly possible the thread will eat up another thread's update.  So it will check to see if the this.AfterModuleInitialized  is the same as the handler (which store the this.AfterModuleInitialized  's old value), if there is no other thread steals the update, then it is safe to proceed, otherwise, it will do another round until it succes. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值