Example #1
0
        public async Task <PolicyResult <TOutput> > ExecuteWithTimeoutAsync <TOutput>(Func <CancellationToken, TOutput> func, int timeout, CancellationToken relatedToken)
        {
            CancellationEnforcer <TOutput> cancellationEnforcer = new CancellationEnforcer <TOutput>();

            // need a little bit of extra time to allow the globalTimer to execute first
            AsyncTimeoutPolicy timeoutPolicy = Policy.TimeoutAsync(timeout + 1, TimeoutStrategy.Optimistic);

            PolicyResult <TOutput> policyResult = await timeoutPolicy.ExecuteAndCaptureAsync(
                async ct => await cancellationEnforcer.AskVinnyToKeepWatch(func, ct), relatedToken
                );

            return(policyResult);
        }