title: C# Threading issue with AutoResetEvent
body: <p>How to properly synchronize this? At the moment it is possible that <code>SetData</code> is called after <code>e.WaitOne()</code> has completed so <code>d</code> could be already set to another value. I tried to insert locks but it resulted into a deadlock. </p>
  
  <pre><code>AutoResetEvent e = new AutoResetEvent(false);
  
  public SetData(MyData d)
  {
     this.d=d;
     e.Set();    // notify that new data is available
  }
  
  // This runs in separate thread and waits for d to be set to a new value
  void Runner() 
  {    
     while (true)
     {
        e.WaitOne();  // waits for new data to process
        DoLongOperationWith_d(d);
     }
  }
  </code></pre>
  
  <p>Will the best solution be to introduce a new boolean variable <code>dataAlreadyBeenSetAndWaitingToBeProcessed</code> that is set in <code>SetData</code> to true and at the end of <code>DoLongOperationWith_d</code> it could be set to true, so if <code>SetData</code> is called with this variable set to true it could just return?</p>