/// <summary> /// Invokes the specified callback repeatedly until either the specified timeout expires or an /// automation element is returned. /// </summary> /// <param name="callback">The get results callback method.</param> /// <param name="timeout">The maximum amount of time to wait for the callback to return an automation element.</param> /// <returns>The automation element, or enumarable, or null if no element was found.</returns> 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); } } }
/// <summary> /// Invokes the specified callback repeatedly until either the specified timeout expires or an /// automation element is returned. /// </summary> /// <param name="callback">The get results callback method.</param> /// <param name="timeout">The maximum amount of time to wait for the callback to return an automation element.</param> /// <returns>The automation element, or enumarable, or null if no element was found.</returns> 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; } } }