Пример #1
0
        /// <summary>
        /// Creates a task that will complete successfully after the given timespan.
        /// </summary>
        /// <param name="p_timespan">The amount of time to wait.</param>
        /// <returns>A new task.</returns>
        public static UnityTask RunDelay(float p_seconds, CancellationToken p_cancellationToken)
        {
            var tcs          = new UnityTaskCompletionSource <int>();
            var cancellation = p_cancellationToken.Register(() => tcs.TrySetCanceled());

            tcs.Task.TaskGenerator = () => DelayCoroutine(p_seconds);
            tcs.Task.ReturnResult  = _ =>
            {
                try
                {
                    tcs.TrySetResult(0);
                    cancellation.Dispose();
                }
                catch (Exception e)
                {
                    tcs.TrySetException(e);
                    cancellation.Dispose();
                }
            };
            if (Thread.CurrentThread.IsBackground)// 如果当前不在前端线程,则切换
            {
                ForegroundInvoker.Invoke(() =>
                {
                    tcs.Task.TaskCoroutine = UnityTaskScheduler.FromCurrentSynchronizationContext().Post(tcs.Task.TaskGenerator(), tcs.Task.ReturnResult);
                });
            }
            else
            {
                tcs.Task.TaskCoroutine = UnityTaskScheduler.FromCurrentSynchronizationContext().Post(tcs.Task.TaskGenerator(), tcs.Task.ReturnResult);
            }
            return(tcs.Task);
        }
Пример #2
0
        /// <summary>
        /// Registers a continuation for the task that will run when the task is complete.
        /// </summary>
        /// <typeparam name="T">The type returned by the continuation.</typeparam>
        /// <param name="p_continuation">The continuation to run after the task completes.
        /// The function takes the completed task as an argument and can return a value.</param>
        /// <returns>A new Task that returns the value returned by the continuation after both
        /// the task and the continuation are complete.</returns>
        public UnityTask ContinueWith(Action <UnityTask> p_continuation, System.Threading.Tasks.CancellationToken p_cancellationToken)
        {
            bool completed = false;

            UnityTaskCompletionSource <int> utcs        = new UnityTaskCompletionSource <int>();
            CancellationTokenRegistration   cancelToken = p_cancellationToken.Register(() => utcs.TrySetCanceled());

            // 此处防止判断为false之后正好执行完毕
            completed = IsCompleted;
            if (!completed)
            {
                m_continuationActions.Add(t =>
                {
                    try
                    {
                        p_continuation(this);
                        utcs.TrySetResult(0);
                        cancelToken.Dispose();
                    }
                    catch (Exception ex)
                    {
                        utcs.TrySetException(ex);
                        cancelToken.Dispose();
                    }
                });
            }
            else
            {
                ForegroundInvoker.Invoke(() =>// 如果当前不在前端线程,则切换
                {
                    try
                    {
                        p_continuation(this);
                        utcs.TrySetResult(0);
                        cancelToken.Dispose();
                    }
                    catch (Exception ex)
                    {
                        utcs.TrySetException(ex);
                        cancelToken.Dispose();
                    }
                });
            }

            return(utcs.Task);
        }
Пример #3
0
    // Use this for initialization
    void Start()
    {
        Debug.Log(string.Format("{0} start:", System.DateTime.Now));

        Debug.Log(string.Format("ForegroudTimer start:", System.DateTime.Now));
        ForegroudTimer timer = new ForegroudTimer(o =>
        {
            Debug.Log(string.Format("{0} ForegroudTimer trigger:", System.DateTime.Now));
        }, null, 1000, 2000);


        ForegroundInvoker.Invoke(() =>
        {
            Debug.Log(string.Format("ForegroundInvoker invoke:", System.DateTime.Now));
        });

        BackgroundProcessor.AppendAction(() =>
        {
            timer.Dispose();
            Debug.Log(string.Format("{0} ForegroudTimer end:", System.DateTime.Now));
        }, 4000, 1, 8000);
    }
Пример #4
0
 /// <summary>
 /// run tasks in continuation list
 /// </summary>
 private void RunContinuations()
 {
     if (Thread.CurrentThread.IsBackground)
     {
         var continueActions = m_continuationActions.ToList();
         ForegroundInvoker.Invoke(() =>
         {
             foreach (var item in continueActions)
             {
                 item(this);
             }
         });
         m_continuationActions.Clear();
     }
     else
     {
         foreach (var item in m_continuationActions)
         {
             item(this);
         }
         m_continuationActions.Clear();
     }
 }
Пример #5
0
        /// <summary>
        /// Registers a continuation for the task that will run when the task is complete.
        /// </summary>
        /// <typeparam name="T">The type returned by the continuation.</typeparam>
        /// <param name="p_continuation">The continuation to run after the task completes.
        /// The function takes the completed task as an argument and can return a value.</param>
        /// <param name="p_cancellationToken">The cancellation token.</param>
        /// <returns>A new Task that returns the value returned by the continuation after both
        /// the task and the continuation are complete.</returns>
        public UnityTask <UnityTask> ContinueWith(Func <UnityTask, UnityTask> p_continuation, System.Threading.Tasks.CancellationToken p_cancellationToken)
        {
            bool completed = false;

            UnityTaskCompletionSource <UnityTask> utcs        = new UnityTaskCompletionSource <UnityTask>();
            CancellationTokenRegistration         cancelToken = p_cancellationToken.Register(() => utcs.TrySetCanceled());

            // 此处防止判断为false之后正好执行完毕
            completed = IsCompleted;
            if (!completed)
            {
                m_continuationActions.Add(t =>
                {
                    //if (t.IsFaulted)
                    //{
                    //    utcs.TrySetException(t.Exception);
                    //    cancelToken.Dispose();
                    //}
                    //else
                    //{
                    try
                    {
                        UnityTask result = p_continuation(t);
                        utcs.TrySetResult(result);
                        cancelToken.Dispose();
                    }
                    catch (Exception ex)
                    {
                        utcs.TrySetException(ex);
                        cancelToken.Dispose();
                    }
                    //}
                });
            }
            else
            {
                ForegroundInvoker.Invoke(() =>
                {
                    //if (this.IsFaulted)
                    //{
                    //    utcs.TrySetException(this.Exception);
                    //    cancelToken.Dispose();
                    //}
                    //else
                    //{
                    try
                    {
                        UnityTask result = p_continuation(this);
                        utcs.TrySetResult(result);
                        cancelToken.Dispose();
                    }
                    catch (Exception ex)
                    {
                        utcs.TrySetException(ex);
                        cancelToken.Dispose();
                    }
                    //}
                });
            }

            return(utcs.Task);
        }