public bool WaitForMessage(TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); // If timeout == TimeSpan.MaxValue, then we want to pass Timeout.Infinite as // SemaphoreSlim doesn't accept timeouts > Int32.MaxValue. // Using TimeoutHelper.RemainingTime() would yield a value less than TimeSpan.MaxValue // and would result in the value Int32.MaxValue so we must use the original timeout specified. if (!_sourceLock.Wait(TimeoutHelper.ToMilliseconds(timeout))) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new TimeoutException(SR.Format(SR.WaitForMessageTimedOut, timeout), TimeoutHelper.CreateEnterTimedOutException(timeout))); } try { return(_source.WaitForMessage(timeoutHelper.RemainingTime())); } finally { _sourceLock.Release(); } }