如何异步重试直到成功或超时?

时间:2013-08-20 13:02:07

标签: c# multithreading task

我写下了这些代码:

public static bool RetryUntilSuccessOrTimeoutAsync(Func<bool> task, 
TimeSpan executionTimeout, CancellationToken? token = null) {
        var data = new ExecutionContextData(task, executionTimeout, token);

        var nonBlockingTask = new Task<bool>(SyncTaskExecutor, data);
        nonBlockingTask.Start();

        var result = nonBlockingTask.ContinueWith(t => t.Result);
        return result.Result;
    }

class ExecutionContextData {
        private readonly Func<bool> task;
        private readonly TimeSpan executionTimeout;
        private readonly CancellationToken? cancellationToken;

        public ExecutionContextData(Func<bool> task, TimeSpan executionTimeout, CancellationToken? cancellationToken) {
            this.cancellationToken = cancellationToken;
            this.executionTimeout = executionTimeout;
            this.task = task;
        }

        public Func<bool> Task {
            get { return task; }
        }

        public TimeSpan ExecutionTimeout {
            get { return executionTimeout; }
        }

        public CancellationToken? CancellationToken {
            get { return cancellationToken; }
        }
    }

private static bool SyncTaskExecutor(object executionHelper) {
        var context = executionHelper as ExecutionContextData;
        Task<bool> newTask = 
context.CancellationToken.HasValue ? new Task<bool>(ExecuteTask, context.Task, context.CancellationToken.Value) 
: new Task<bool>(ExecuteTask, context.Task);
        newTask.Start();

        bool timeoutResult = newTask.Wait(context.ExecutionTimeout);
        if (timeoutResult)
            return newTask.Result;
        return false;
    }

但据我所知,Result属性invokation将阻止调用者。所以,我完全不明白如何完成这项任务: “如何异步执行任务,所以如果超时则会返回false,否则会返回应该反复执行的任务结果?”

1 个答案:

答案 0 :(得分:1)

如果您有可能想要取消或暂停的操作,为什么不尝试这样的事情:

public static class Retries
{
    public enum Result
    {
        Success,
        Timeout, 
        Canceled,
    }

    public static Task<Result> RetryUntilTimedOutOrCanceled(this Func<bool> func, CancellationToken cancel, TimeSpan timeOut)
    {
        return Task.Factory.StartNew(() =>
        {
            var start = DateTime.UtcNow;
            var end = start + timeOut;
            while (true)
            {
                var now = DateTime.UtcNow;
                if (end < now)
                    return Result.Timeout;
                var curTimeOut = end - now;
                Task<bool> curTask = null;
                try
                {
                    if (cancel.IsCancellationRequested)
                        return Result.Canceled;
                    curTask = Task.Factory.StartNew(func, cancel);
                    curTask.Wait((int)curTimeOut.TotalMilliseconds, cancel);
                    if (curTask.IsCanceled)
                        return Result.Canceled;
                    if (curTask.Result == true)
                        return Result.Success;
                }
                catch (TimeoutException)
                {
                    return Result.Timeout;
                }
                catch (TaskCanceledException)
                {
                    return Result.Canceled;
                }
                catch (OperationCanceledException)
                {
                    return Result.Canceled;
                }
            }
        });
    }
}
class Program
{

    static void Main(string[] args)
    {
        var cancelSource = new CancellationTokenSource();
        Func<bool> AllwaysFalse = () => false;
        Func<bool> AllwaysTrue = () => true;

        var result = AllwaysFalse.RetryUntilTimedOutOrCanceled(cancelSource.Token, TimeSpan.FromSeconds(3)).Result;
        Console.WriteLine(result);

        result = AllwaysTrue.RetryUntilTimedOutOrCanceled(cancelSource.Token, TimeSpan.FromSeconds(3)).Result;
        Console.WriteLine(result);

        var rTask = AllwaysFalse.RetryUntilTimedOutOrCanceled(cancelSource.Token, TimeSpan.FromSeconds(100));
        System.Threading.Thread.Sleep(1000);
        cancelSource.Cancel();
        result = rTask.Result;
        Console.WriteLine(result);

        Console.ReadLine();
    }
}