title: Blocking and waiting for an event
body: <p>It sometimes want to block my thread while waiting for a event to occur.</p>
  
  <p>I usually do it something like this:</p>
  
  <pre><code>private AutoResetEvent _autoResetEvent = new AutoResetEvent(false);
  
  private void OnEvent(object sender, EventArgs e){
    _autoResetEvent.Set();
  }
  
  // ...
  button.Click += OnEvent;
  try{
    _autoResetEvent.WaitOne();
  }
  finally{
    button.Click -= OnEvent;
  }
  </code></pre>
  
  <p>However, it seems that this should be something that I could extract to a common class (or perhaps even something that already exists in the framework).</p>
  
  <p>I would like to be able to do something like this:</p>
  
  <pre><code>EventWaiter ew = new EventWaiter(button.Click);
  ew.WaitOne();
  EventWaiter ew2 = new EventWaiter(form.Closing);
  ew2.WaitOne();
  </code></pre>
  
  <p>But I can't really find a way to construct such a class (I can't find a good valid way to pass the event as an argument). Can anyone help?</p>
  
  <p>To give an example of why this can be useful, consider something like this:</p>
  
  <pre><code>var status = ShowStatusForm();
  status.ShowInsertUsbStick();
  bool cancelled = WaitForUsbStickOrCancel();
  if(!cancelled){
    status.ShowWritingOnUsbStick();
    WriteOnUsbStick();
    status.AskUserToRemoveUsbStick();
    WaitForUsbStickToBeRemoved();
    status.ShowFinished();
  }else{
    status.ShowCancelled();
  }
  status.WaitUntilUserPressesDone();
  </code></pre>
  
  <p>This is much more concise and readable than the equivalent code written with the logic spread out between many methods. But to implement WaitForUsbStickOrCancel(), WaitForUsbStickToBeRemoved and WaitUntilUserPressesDone() (assume that the we get an event when usb sticks are inserted or removed) I need to reimplement "EventWaiter" each time. Of course you have to be careful to never run this on the GUI-thread, but sometimes that is a worthwhile tradeoff for the simpler code.</p>
  
  <p>The alternative would look something like this:</p>
  
  <pre><code>var status = ShowStatusForm();
  status.ShowInsertUsbStick();
  usbHandler.Inserted += OnInserted;
  status.Cancel += OnCancel;
  //...
  void OnInserted(/*..*/){
    usbHandler.Inserted -= OnInserted;
    status.ShowWritingOnUsbStick();
    MethodInvoker mi = () =&gt; WriteOnUsbStick();
    mi.BeginInvoke(WritingDone, null);
  }
  void WritingDone(/*..*/){
    /* EndInvoke */
    status.AskUserToRemoveUsbStick();
    usbHandler.Removed += OnRemoved;
  }
  void OnRemoved(/*..*/){
    usbHandler.Removed -= OnRemoved;
    status.ShowFinished();
    status.Done += OnDone;
  }
  /* etc */
  </code></pre>
  
  <p>I find that much harder to read. Admittedly, it is far from always that the flow will be so linear, but when it is, I like the first style.</p>
  
  <p>It is comparable to using ShowMessage() and Form.ShowDialog() - they also block until some "event" occurs (though they will run a message-loop if they are called on the gui-thread).</p>