/// <summary> /// Check that we did not timed out. /// If the timeout was not started, starts it /// </summary> public void CheckTimeOut(string reason = "") { this.Start(); stopWatch.Stop(); totalElapsed += stopWatch.Elapsed; stopWatch.Reset(); stopWatch.Start(); // If we've reached a timeout, we throw an exception, and we abort the fixpoint computation if (totalElapsed.TotalSeconds >= timeout #if DEBUG && !System.Diagnostics.Debugger.IsAttached #endif ) { #if DEBUG if (!String.IsNullOrEmpty(reason)) { Console.WriteLine("Timeout hit: Reason {0}", reason); } #endif if (exception == null) { exception = new TimeoutExceptionFixpointComputation(); } throw exception; } cancellationToken.ThrowIfCancellationRequested(); }
/// <param name="seconds"> The timeout in seconds</param> public TimeOutChecker(int seconds, CancellationToken cancellationToken, bool start = true) { Contract.Requires(seconds >= 0); stopWatch = new CustomStopwatch(); totalElapsed = new TimeSpan(); if (start) { stopWatch.Start(); state = State.Running; } else { state = State.Stopped; } timeout = seconds; this.cancellationToken = cancellationToken; exception = null; }