title: Block code and wait for event handler to fire?
body: <p>I'm writing an application that uses a 3rd party library to process some data.  In a very simplified example, I have a method which runs as a task like this:</p>
  
  <pre><code>private void ProcessListOfItems(List&lt;string&gt; items)
  { 
      while (items.Count &gt; 0)
      {
          3rdPartyLibObject.Process(items[0]);
          items.Remove(0);
      }
  }
  </code></pre>
  
  <p>As you can see, the way my code is currently written, I remove each item from the list as soon as the Process() method returns.  But it's possible that the processing of an item could fail and I need to know if that happens.  Unfortunately, the Process() method does not return a bool value to indicate that the item was successfully processed or not but rather it will fire ProcessingComplete and ProcessingFailed events.  I have event handlers hooked up to those events like this:</p>
  
  <pre><code>3rdPartyLibObject.ProcessingComplete += obj_ProcessingSuccess;
  3rdPartyLibObject.ProcessingFailed += obj_ProcessingFailed;
  
  private void obj_ProcessingSuccess(object sender, 3rdPartyLibObject.ProcessingEventArgs e)
  {
      this.Invoke(new ProcessedHandler(OnProcessed), new object[] { true });
  }
  
  private void obj_ProcessingFailed(object sender, 3rdPartyLibObject.ProcessingEventArgs e)
  {
      this.Invoke(new ProcessedHandler(OnProcessed), new object[] { false });
  }
  
  private void OnProcessed(bool success)
  {
      if (success)
      {
          Debug.WriteLine("Item was processed succesfully!");
      }
      else
      {
          Debug.WriteLine("Failed to process item!");
      }
  }
  </code></pre>
  
  <p>What I'd like to do is have my code block right after the call to 3rdPartyLibObject.Process() and until one of the event handlers fires so I know if the item failed to process or not (and whether I should remove it from the list or not).  I'm guessing that this is probably not an uncommon situation but I've never run into it before.  Is there a generally agreed upon best practice for dealing with this kind of scenario?</p>