/// <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); }
/// <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); }