/// <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));
            }
        }
示例#2
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 != null)
                {
                    taskSource.TrySetException(e.Error);
                }
                else if (e.WasCancelled)
                {
                    taskSource.TrySetCanceled();
                }
                else
                {
                    var rr = s as ICoTask <TResult>;
                    taskSource.TrySetResult(rr != null ? 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);
        }