Ejemplo n.º 1
0
 public void AsyncMethodCallbackTest()
 {
     bool methodCalled = false;
     // setup method
     Func<int, int> method = n =>
     {
         methodCalled = true;
         return -1;
     };
     // setup test instance
     var asyncSample = new AsyncBench(
         20, method);
     IAsyncResult ar = method.BeginInvoke(20, null, new State {Field = 10});
     // wait for the method to complete
     ar.AsyncWaitHandle.WaitOne();
     // verify it has completed
     Assert.IsTrue(ar.IsCompleted, "Operation should have completed before reaching this point!");
     // and was really called
     Assert.IsTrue(methodCalled, "Test method should have been called, but it was not!");
     // use IAsyncResult from our own BeginInvoke for the callback on the test instance
     asyncSample.AsyncMethodCallback(ar);
     // check that EndInvoke worked as expected
     Assert.AreEqual(-1, asyncSample.Param);
 }