title: Waiting for Event Triggering in Silverlight Unit Tests
body: <p>I am using the Silverlight Unit Testing Framework to test some View Manager classes. Some tests require the <i>PropertyChanged</i> events to be fired.</p>
  
  <p>I'm currently using a combination of <i>EnqueueConditional</i> and <i>WaitHandles</i></p>
  
  <p><strong>Example 1</strong></p>
  
  <pre><code>[TestMethod]
  [Asynchronous]
  [Timeout(1000)]
  public void TestNotificationExample()
  {
      var manager = new UserManager();
  
      var waitHandle = new ManualResetEvent(false);
      manager.PropertyChanged += (sender, propChangArgs) =&gt;
                                  {
                                      waitHandler.Set();
                                  };
      manager.DoTheThingThatTriggersNotification();
      // The notification event fires aynshronously to this
      EnqueueConditional (() =&gt; waitHandler.WaitOne(0));
      // Enqueue other tests here....
      EnqueueTestComplete();
  }
  </code></pre>
  
  <p>This works. But I've got questions nagging at me:</p>
  
  <p>Do I actually need to use a WaitHandle? Would it perform equally as well if I just used a bool?</p>
  
  <p><strong>Example 2</strong></p>
  
  <pre><code>bool fHasFiredEvent = false;
  manager.PropertyChanged += (sender, propChangeArgs) =&gt;
                             { 
                                 fHasFiredEvent = true;
                             }
  manager.DoTheThingThatTriggersNotification();    
  EnqueueConditional (() =&gt; fHasFiredEvent);
  EnqueueTestComplete();
  </code></pre>
  
  <p>Or would it be better if I kept the WaitHandle, but lost the TimeoutAttribute and timed out on the Wait?</p>
  
  <p><strong>Example 3</strong></p>
  
  <pre><code>[TestMethod]
  [Asynchronous]
  public void TestNotificationExample()
  {
      var manager = new UserManager();
  
      var waitHandle = new ManualResetEvent(false);
      manager.PropertyChanged += (sender, propChangArgs) =&gt;
                                  {
                                      waitHandler.Set();
                                  };
      manager.DoTheThingThatTriggersNotification();
      EnqueueCallback (() =&gt; Assert.IsTrue(waitHandler.WaitOne(1000));
      EnqueueTestComplete();
  }
  </code></pre>
  
  <p>So now I've written three examples, and they all work. So my final question is</p>
  
  <ul>
  <li>Which would have the best
  performance? (Even though the
  difference is negligible and it's
  purely academic yada yada yada. It's interesting for its own sake.) </li>
  <li>Do any of the three examples have
  fundamental flaws?</li>
  </ul>