Пример #1
0
        /// <summary>
        /// Executes the coTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override async void BeginExecute(CoroutineExecutionContext context)
        {
            try
            {
                await _innerTask;
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch { }

            OnCompleted(_innerTask);
        }
Пример #2
0
 /// <summary>
 /// Called when the execution of the decorated CoTask has completed.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="innerCoTask">The decorated CoTask.</param>
 /// <param name="args">The <see cref="CoTaskCompletedEventArgs" /> instance containing the event data.</param>
 protected override void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask,
                                                CoTaskCompletedEventArgs args)
 {
     if (args.Error is object || !args.WasCancelled)
     {
         OnCompleted(new CoTaskCompletedEventArgs(args.Error, false));
     }
     else
     {
         Continue(context);
     }
 }
Пример #3
0
        /// <summary>
        /// Executes the CoTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            _context = context;

            try
            {
                _innerCoTask.Completed += InnerCoTaskCompleted;
                _innerCoTask.BeginExecute(_context);
            }
            catch (Exception ex)
            {
                InnerCoTaskCompleted(_innerCoTask, new CoTaskCompletedEventArgs(ex, false));
            }
        }
        /// <summary>
        /// Called when the execution of the decorated CoTask has completed.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="innerCoTask">The decorated coTask.</param>
        /// <param name="args">The <see cref="Caliburn.Coroutines.CoTaskCompletedEventArgs" /> instance containing the event data.</param>
        protected override void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask,
                                                       CoTaskCompletedEventArgs args)
        {
            var error = args.Error as TException;

            if (error is null)
            {
                OnCompleted(args);
            }
            else
            {
                Rescue(context, error);
            }
        }
Пример #5
0
        /// <summary>
        /// Executes the CoTask using the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void BeginExecute(CoroutineExecutionContext context)
        {
            Exception error = null;

            try
            {
                _toExecute();
            }
            catch (Exception ex)
            {
                error = ex;
            }

            OnCompleted(new CoTaskCompletedEventArgs(error, false));
        }
Пример #6
0
        private static Task <TResult> InternalExecuteAsync <TResult>(ICoTask coTask, CoroutineExecutionContext context)
        {
            var taskSource = new TaskCompletionSource <TResult>();

            EventHandler <CoTaskCompletedEventArgs> completed = null;

            completed = (s, e) =>
            {
                ((ICoTask)s).Completed -= completed;

                if (e.Error is object)
                {
                    taskSource.TrySetException(e.Error);
                }
                else if (e.WasCancelled)
                {
                    taskSource.TrySetCanceled();
                }
                else
                {
                    var rr = s as ICoTask <TResult>;
                    taskSource.TrySetResult(rr is object?rr.Result: default(TResult));
                }
            };

            try
            {
                coTask.Completed += completed;
                coTask.BeginExecute(context ?? new CoroutineExecutionContext());
            }
            catch (Exception ex)
            {
                coTask.Completed -= completed;
                taskSource.TrySetException(ex);
            }

            return(taskSource.Task);
        }
Пример #7
0
        private void Continue(CoroutineExecutionContext context)
        {
            ICoTask continueCoTask;

            try
            {
                continueCoTask = _coroutine();
            }
            catch (Exception ex)
            {
                OnCompleted(new CoTaskCompletedEventArgs(ex, false));
                return;
            }

            try
            {
                continueCoTask.Completed += ContinueCompleted;
                continueCoTask.BeginExecute(context);
            }
            catch (Exception ex)
            {
                ContinueCompleted(continueCoTask, new CoTaskCompletedEventArgs(ex, false));
            }
        }
        private void Rescue(CoroutineExecutionContext context, TException exception)
        {
            ICoTask rescueCoTask;

            try
            {
                rescueCoTask = _coroutine(exception);
            }
            catch (Exception ex)
            {
                OnCompleted(new CoTaskCompletedEventArgs(ex, false));
                return;
            }

            try
            {
                rescueCoTask.Completed += RescueCompleted;
                rescueCoTask.BeginExecute(context);
            }
            catch (Exception ex)
            {
                RescueCompleted(rescueCoTask, new CoTaskCompletedEventArgs(ex, false));
            }
        }
Пример #9
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public abstract void BeginExecute(CoroutineExecutionContext context);
Пример #10
0
 private void OnComplete(Exception error, bool wasCancelled)
 {
     _context = null;
     _enumerator.Dispose();
     OnCompleted(new CoTaskCompletedEventArgs(error, wasCancelled));
 }
Пример #11
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public override void BeginExecute(CoroutineExecutionContext context)
 {
     _context = context;
     ChildCompleted(null, new CoTaskCompletedEventArgs(null, false));
 }
Пример #12
0
 /// <summary>
 /// Called when the execution of the decorated CoTask has completed.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="innerCoTask">The decorated CoTask.</param>
 /// <param name="args">The <see cref="CoTaskCompletedEventArgs"/> instance containing the event data.</param>
 protected abstract void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask,
                                                CoTaskCompletedEventArgs args);
Пример #13
0
 private void InnerCoTaskCompleted(object sender, CoTaskCompletedEventArgs args)
 {
     _innerCoTask.Completed -= InnerCoTaskCompleted;
     OnInnerResultCompleted(_context, _innerCoTask, args);
     _context = null;
 }
Пример #14
0
 /// <summary>
 /// Executes an <see cref="ICoTask&lt;TResult&gt;"/> asynchronous.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="coTask">The coroutine to execute.</param>
 /// <param name="context">The context to execute the coroutine within.</param>
 /// <returns>A task that represents the asynchronous coroutine.</returns>
 public static Task <TResult> ExecuteAsync <TResult>(this ICoTask <TResult> coTask,
                                                     CoroutineExecutionContext context = null)
 {
     return(InternalExecuteAsync <TResult>(coTask, context));
 }
Пример #15
0
 /// <summary>
 /// Executes an <see cref="ICoTask"/> asynchronous.
 /// </summary>
 /// <param name="coTask">The coroutine to execute.</param>
 /// <param name="context">The context to execute the coroutine within.</param>
 /// <returns>A task that represents the asynchronous coroutine.</returns>
 public static Task ExecuteAsync(this ICoTask coTask, CoroutineExecutionContext context = null)
 {
     return(InternalExecuteAsync <object>(coTask, context));
 }
Пример #16
0
 /// <summary>
 /// Executes the CoTask using the specified context.
 /// </summary>
 /// <param name="context">The context.</param>
 public override void BeginExecute(CoroutineExecutionContext context)
 {
     OnCompleted(new CoTaskCompletedEventArgs(_error, _wasCancelled));
 }