async UniTaskVoid RunDelayAsync(TimeSpan timeout) { timeoutDelay = StoppableDelayRealtimePromise.Create(timeout, PlayerLoopTiming.Update, (linkedSource == null) ? CancellationToken.None : linkedSource.Token, out var version); try { var reason = await new UniTask <DelayResult>(timeoutDelay, version); if (reason == DelayResult.DelayCompleted) { // UnityEngine.Debug.Log("DEBUG:Timeout Complete, try to call timeoutSource.Cancel"); timeoutSource.Cancel(); } else if (reason == DelayResult.LinkedTokenCanceled) { // UnityEngine.Debug.Log("DEBUG:LinkedSource IsCancellationRequested"); } else if (reason == DelayResult.ExternalStopped) { // Reset(Promise.Stop) called, do nothing. // UnityEngine.Debug.Log("DEBUG:Reset called"); } } finally { timeoutDelay = null; } }
public TimeoutController() { this.timeoutSource = new CancellationTokenSource(); this.originalLinkCancellationTokenSource = null; this.linkedSource = null; this.timeoutDelay = null; }
public TimeoutController(CancellationTokenSource linkCancellationTokenSource) { this.timeoutSource = new CancellationTokenSource(); this.originalLinkCancellationTokenSource = linkCancellationTokenSource; this.linkedSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutSource.Token, linkCancellationTokenSource.Token); this.timeoutDelay = null; }
public void Reset() { if (timeoutDelay != null) { timeoutDelay.Stop(); // stop delay, will finish RunDelayAsync timeoutDelay = null; } }
public static StoppableDelayRealtimePromise Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) { if (!pool.TryPop(out var result)) { result = new StoppableDelayRealtimePromise(); } result.stopwatch = ValueStopwatch.StartNew(); result.delayTimeSpanTicks = delayTimeSpan.Ticks; result.cancellationToken = cancellationToken; result.externalStop = false; TaskTracker.TrackActiveTask(result, 3); PlayerLoopHelper.AddAction(timing, result); token = result.core.Version; return(result); }