Esempio n. 1
0
        /// <summary>
        /// Wraps the non-generic <see cref="T:System.Threading.Tasks.Task" /> into a generic <see cref="T:System.Threading.Tasks.Task" />.
        /// </summary>
        /// <param name="taskAction">The task to wrap.</param>
        /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> that wraps the non-generic <see cref="T:System.Threading.Tasks.Task" />.</returns>
        private static Task <bool> StartAsGenericTask(Func <Task> taskAction)
        {
            Task task = taskAction();

            if (task == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.TaskCannotBeNull, new object[]
                {
                    "taskAction"
                }), "taskAction");
            }
            if (task.Status == TaskStatus.RanToCompletion)
            {
                return(AsyncExecution.GetCachedTask());
            }
            if (task.Status == TaskStatus.Created)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.TaskMustBeScheduled, new object[]
                {
                    "taskAction"
                }), "taskAction");
            }
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

            task.ContinueWith(delegate(Task t)
            {
                if (t.IsFaulted)
                {
                    tcs.TrySetException(t.Exception.InnerExceptions);
                    return;
                }
                if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                    return;
                }
                tcs.TrySetResult(true);
            }, TaskContinuationOptions.ExecuteSynchronously);
            return(tcs.Task);
        }
Esempio n. 2
0
 public AsyncExecution(Func <Task> taskAction, ShouldRetry shouldRetry, Func <Exception, bool> isTransient, Action <int, Exception, TimeSpan> onRetrying, bool fastFirstRetry, CancellationToken cancellationToken) : base(() => AsyncExecution.StartAsGenericTask(taskAction), shouldRetry, isTransient, onRetrying, fastFirstRetry, cancellationToken)
 {
 }