title: What happens if C# event handler disposed and I call it?
body: <p>This question is a follow up to the <a href="https://stackoverflow.com/questions/786383/c-sharp-events-and-thread-safety">C# Events and Thread Safety</a> question (I am not the author of that one) and the related blog post by Eric Lippert <a href="http://blogs.msdn.com/b/ericlippert/archive/2009/04/29/events-and-races.aspx" rel="nofollow noreferrer">Events and Races</a>. There are other similar questions on SO, but none of them actually consider this case, general consensus is that so long as you unsubscribe you are safe, but I don't believe that is true all the time.</p>
  
  <p>According to the discussion in both the SO question and the blog, the pattern that should be used is something like:</p>
  
  <pre><code>var ev = NotifyPropertyChanged;
  if (ev != null)
      ev(this, new PropertyChangedEventArgs("Foo"));
  </code></pre>
  
  <p>But what if the following situation occurs:<br>
  1) I subscribe a listener:</p>
  
  <pre><code>mytype.NotifyPropertyChanged += Handler; // Handler is instance method in SomeObject class
  </code></pre>
  
  <p>2) I (or the run-time, due to scoping) dispose SomeObject, which contains the listener and unsubscribe the listener, at about the same time the property notification happens.</p>
  
  <p>3) While unlikely due to very short time period this can happen in, it is theoretically possible that because <strong>ev</strong> preserves the old subscriber that is no longer in existence, it will call a function in an object that no longer exist.</p>
  
  <p>According to the Eric Lippert, "<em>event handlers are required to be robust in the face of being called even after the event has been unsubscribed</em>". But if the handler is unsubscribed <strong>and disposed</strong>, it can no longer take care of the call. What is the correct way to handle this situation?</p>
  
  <p>Wrap the code from (1) in try-catch? What exception should be caught? ObjectDisposedException seems likely, but not the only one that can happen, I think.</p>