protected object ExecuteGetResult(GetResultCallback callback, TimeSpan timeout)
{
var start = DateTime.Now;
var timeoutRemaining = (int) timeout.TotalMilliseconds;
while (true) {
var result = callback();
if (result != null) return result;
if (timeoutRemaining > 0) {
// The amount of time slept will never be more than the MaxSleepTimeout constant.
// The total time spent sleeping will also never be more than the total timeout specified.
var actualSleepTimeout = timeoutRemaining > MaxSleepTimeout ? MaxSleepTimeout : timeoutRemaining;
Thread.Sleep(actualSleepTimeout);
// The timeout is decremented so that the remaining time is the total time - elapsed time.
timeoutRemaining -= (int) (DateTime.Now - start).TotalMilliseconds;
} else {
// Time is up and still no element. So return null.
return null;
}
}
}