title: Unit test passes in debug build but fails in release build
body: <p>I have written a unit test for an asynchronous class member. The test passes as expected when I execute my test under "Debug build". However, it hangs the CPU (while loop is deadlocked) when I execute my test under "Release build". </p>
  
  <p>If I specifically configure the Unit test project with Debug build (that is Debug build unit test assembly and Release build target assembly), the test passes as well. </p>
  
  <p>Code to Test</p>
  
  <pre><code>    public override void DoSomething(object parameter)
      {
          ThreadPool.QueueUserWorkItem(AsyncDoSomething, parameter);
      }
  
      private void AsyncDoSomething(object parameter)
      {
          //Doing something
                  .....
  
          //Something is done
          RaiseSomethingIsDone();
      }
  </code></pre>
  
  <p>My Unit Test</p>
  
  <pre><code>    public void DoingSomethingTest()
      {
          bool IsSomethingDone = false;
  
          //Setup
          //Doing some setup here.
          target.SomethingDone += (sender, args) =&gt;
              {
                  IsSomethingDone = true;
              };
  
          //Exercise
          target.DoSomething(_someParameter);
          while (!IsSomethingDone ){}
  
          //Verify
          //Doing some asserts here.
      }
  </code></pre>
  
  <p>Here is the IL generated by the C# compiler under Debug configuration and Release configuration: </p>
  
  <p>Debug while loop IL interpenetration:</p>
  
  <pre><code>  IL_00cb:  ldloc.s    'CS$&lt;&gt;8__locals7'
    IL_00cd:  ldfld      bool IsSomethingDone
    IL_00d2:  ldc.i4.0
    IL_00d3:  ceq
    IL_00d5:  stloc.s    CS$4$0001
    IL_00d7:  ldloc.s    CS$4$0001
    IL_00d9:  brtrue.s   IL_00c9
  </code></pre>
  
  <p>Release while loop IL interpenetration:</p>
  
  <pre><code>  IL_00bc:  ldloc.s    'CS$&lt;&gt;8__locals7'
    IL_00be:  ldfld      bool IsSomethingDone
    IL_00c3:  brfalse.s  IL_00bc
  </code></pre>
  
  <p>I understand there are better ways to synchronize the test thread to the background ThreadPool thread. </p>
  
  <p>My questions are </p>
  
  <ol>
  <li><p>why is the release build not working? The flag IsSomethingDone is not set by the worker thread. </p></li>
  <li><p>Is it because the eventhandler (the lambda expression) does not get executed? </p></li>
  <li><p>Is the event not raised correctly?</p></li>
  </ol>
  
  <p>By the way, I verified that DoSomething is executed correctly and generated the correct result. </p>
  
  <p><strong>Follow up Questions:</strong></p>
  
  <ol>
  <li><p>Should one build unit test projects under Debug Build or Release Build ? </p></li>
  <li><p>Should one test the target assembly under Debug Build or Release Build ?</p></li>
  </ol>