title: How to avoid losing receiver when sending notification?
body: <h2>How my programs works</h2>
  
  <p>I have backend with several objects, which have PropetyChanged event. When some property is changed, the notification is fired. On the frontend side I have GUI written in WPF, and several properties are binded directly to WPF widgets.</p>
  
  <p>To make GUI responsive, all the computation is done in additional thread.</p>
  
  <p>So, in general, classic WPF app.</p>
  
  <h2>Error</h2>
  
  <pre><code>public void OnPropertyChanged(string name)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(name)); // (*)
  }
  </code></pre>
  
  <p>This code sends notifications, however it throws exception at (*) because PropertyChanged is null (yes, just <strong>after</strong> checking for null).</p>
  
  <h2>Question</h2>
  
  <p>My guess is it is because of rebinding in WPF -- in my code I change the sender, so WPF has to unregister receiver and register it to the new sender. Exception is thrown rarely (I get one exception every ~40000 executions passes, each one is equivalent roughly to one change of the sender) but it <strong>does</strong> happen.</p>
  
  <p>Now -- <strong>how to properly lock PropertyChange, so I can check for null AND send the notification as an atomic operation?</strong> I don't want to do this by trial &amp; error, because I could find out side effects in every 1M-th execution.</p>