C# Events and Thread Safety

前端 未结 15 1125
甜味超标
甜味超标 2020-11-22 06:00

UPDATE

As of C# 6, the answer to this question is:

SomeEvent?.Invoke(this, e);

I frequently hear/read the fo

相关标签:
15条回答
  • 2020-11-22 06:27

    So I'm a little late to the party here. :)

    As for the use of null rather than the null object pattern to represent events with no subscribers, consider this scenario. You need to invoke an event, but constructing the object (EventArgs) is non-trivial, and in the common case your event has no subscribers. It would be beneficial to you if you could optimize your code to check to see if you had any subscribers at all before you committed processing effort to constructing the arguments and invoking the event.

    With this in mind, a solution is to say "well, zero subscribers is represented by null." Then simply perform the null check before performing your expensive operation. I suppose another way of doing this would have been to have a Count property on the Delegate type, so you'd only perform the expensive operation if myDelegate.Count > 0. Using a Count property is a somewhat nice pattern that solves the original problem of allowing optimization, and it also has the nice property of being able to be invoked without causing a NullReferenceException.

    Keep in mind, though, that since delegates are reference types, they are allowed to be null. Perhaps there was simply no good way of hiding this fact under the covers and supporting only the null object pattern for events, so the alternative may have been forcing developers to check both for null and for zero subscribers. That would be even uglier than the current situation.

    Note: This is pure speculation. I'm not involved with the .NET languages or CLR.

    0 讨论(0)
  • 2020-11-22 06:29

    I see a lot of people going toward the extension method of doing this ...

    public static class Extensions   
    {   
      public static void Raise<T>(this EventHandler<T> handler, 
        object sender, T args) where T : EventArgs   
      {   
        if (handler != null) handler(sender, args);   
      }   
    }
    

    That gives you nicer syntax to raise the event ...

    MyEvent.Raise( this, new MyEventArgs() );
    

    And also does away with the local copy since it is captured at method call time.

    0 讨论(0)
  • 2020-11-22 06:29

    I've never really considered this to be much of an issue because I generally only protect against this sort of potential threading badness in static methods (etc) on my reusable components, and I don't make static events.

    Am I doing it wrong?

    0 讨论(0)
提交回复
热议问题