C# EventHandler正确地引发事件

C# EventHandler正确地引发事件,c#,events,event-handling,C#,Events,Event Handling,我试图更好地理解事件及其处理程序是如何工作的,但我不明白为什么在引发事件时,通常更倾向于引发相同的事件,即我们的事件本身。 更具体地说,当查看msdn doc()时,它如下所示: class Counter { private int threshold; private int total; public Counter(int passedThreshold) { threshold = passedThreshold; }

我试图更好地理解事件及其处理程序是如何工作的,但我不明白为什么在引发事件时,通常更倾向于引发相同的事件,即我们的事件本身。 更具体地说,当查看msdn doc()时,它如下所示:

class Counter
{
    private int threshold;
    private int total;

    public Counter(int passedThreshold)
    {
        threshold = passedThreshold;
    }

    public void Add(int x)
    {
        total += x;
        if (total >= threshold)
        {
            ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
            args.Threshold = threshold;
            args.TimeReached = DateTime.Now;
            OnThresholdReached(args);
        }
    }

    protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
    {
        EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
}
我们为什么要创建这个“处理程序”?

考虑以下代码:

    if (ThresholdReached!= null)
    {
        ThresholdReached(this, e);
    }
如果在
if(thresholdReach!=null)
之后,但在
thresholdReach(this,e)之前,删除
thresholdReach
的处理程序,多线程代码中会发生什么情况被调用了吗

获取处理程序的副本可防止这种情况发生,并使事件的引发线程安全。

请考虑以下代码:

    if (ThresholdReached!= null)
    {
        ThresholdReached(this, e);
    }
如果在
if(thresholdReach!=null)
之后,但在
thresholdReach(this,e)之前,删除
thresholdReach
的处理程序,多线程代码中会发生什么情况被调用了吗


获取处理程序的副本可以防止这种情况发生,并使事件线程的引发变得安全。

@MattWilko-我认为这是错误的。我很肯定OP中的建议会奏效。我相信获取处理程序的“副本”的原因与线程安全有关。我记得在过去看到过这个完全相同的问题,但我记不清我的结论是什么lol…我的“建议”确实有效,但我很确定他们想要创建这个处理程序有一个很好的理由;)微软文档在这里:没有“复制”,所以如果有什么事情发生,微软显然已经足够微妙地不一致了。@MattWilko-我认为这是错误的。我很肯定OP中的建议会奏效。我相信获取处理程序的“副本”的原因与线程安全有关。我记得在过去看到过这个完全相同的问题,但我记不清我的结论是什么lol…我的“建议”确实有效,但我很确定他们想要创建这个处理程序有一个很好的理由;)微软文档在这里:它没有“复制”,所以如果有什么事情发生,它显然是微妙的足够微软不一致。