Delay() public method

Suspends the current thread by the previously specified length of time, and executes the specified callback when finished.
public Delay ( Callback callback ) : object
callback Callback The Callback to be executed when the delay is complete.
return object
        private void TestSubsequentDelay(Delayer delayer, int delay)
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);

            bool called = false;
            DateTime timeCalled = DateTime.MaxValue;
            Object obj = new Object();

            Callback nop = delegate()
            {
                return null;
            };

            Callback callback = delegate()
            {
                called = true;
                timeCalled = DateTime.Now;
                manualEvent.Set();
                return obj;
            };

            // This is the difference between this method and TestImmediateDelay.
            // This method ignores the first call to Delay and tests the delay of the
            // second call to Delay.
            delayer.Delay(nop);

            DateTime timeStarted = DateTime.Now;

            Object result = delayer.Delay(callback);

            manualEvent.WaitOne(delay * 2, false);

            int timeElapsed = (int)(timeCalled - timeStarted).TotalMilliseconds;
            double elapsedError = delay * TIME_DIFFERENCE;

            Assert.IsTrue(called);
            Assert.AreSame(obj, result);

            Assert.IsTrue(timeElapsed >= delay - elapsedError,
                "Delay was not at least (" + delay + " - " + elapsedError + ") msec; total time elapsed was " + timeElapsed);

            if (timeElapsed > delay + elapsedError)
            {
                Assert.Inconclusive("Test failed, or specified delay was too small to be supported. Time elapsed was " + timeElapsed + " msec; expected <= " + elapsedError);
            }
        }
        private void TestImmediateDelay(Delayer delayer, int delay)
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);

            bool called = false;
            DateTime timeCalled = DateTime.MaxValue;
            Object obj = new Object();

            Callback callback = delegate()
            {
                called = true;
                timeCalled = DateTime.Now;
                manualEvent.Set();

                return obj;
            };

            DateTime timeStarted = DateTime.Now;
            Object result = delayer.Delay(callback);

            manualEvent.WaitOne(delay * 2, false);

            int timeElapsed = (int)(timeCalled - timeStarted).TotalMilliseconds;

            Assert.IsTrue(called);
            Assert.AreSame(obj, result);

            if (timeElapsed > delay * IMMEDIATE_TIME_DIFFERENCE)
            {
                Assert.Inconclusive("Test failed, or delay was too small. Time elapsed was " + timeElapsed + " msec; expected <= " + (delay * IMMEDIATE_TIME_DIFFERENCE));
            }
        }