Automation.UI.Tree.SearchEngine.ExecuteGetResult C# (CSharp) Method

ExecuteGetResult() protected method

Invokes the specified callback repeatedly until either the specified timeout expires or an automation element is returned.
protected ExecuteGetResult ( GetResultCallback callback, System.TimeSpan timeout ) : object
callback GetResultCallback The get results callback method.
timeout System.TimeSpan The maximum amount of time to wait for the callback to return an automation element.
return object
        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;
                }
            }
        }