diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs index 3f2a983..01a3056 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ContinuationQueue.cs @@ -70,13 +70,17 @@ namespace Cysharp.Threading.Tasks.Internal } } - public void Clear() + public int Clear() { + var rest = actionListCount + waitingListCount; + actionListCount = 0; actionList = new Action[InitialSize]; waitingListCount = 0; waitingList = new Action[InitialSize]; + + return rest; } // delegate entrypoint. diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs index b459861..a1dd3cd 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/PlayerLoopRunner.cs @@ -48,14 +48,24 @@ namespace Cysharp.Threading.Tasks.Internal } } - public void Clear() + public int Clear() { lock (arrayLock) { + var rest = 0; + for (var index = 0; index < loopItems.Length; index++) { + if (loopItems[index] != null) + { + rest++; + } + loopItems[index] = null; } + + tail = 0; + return rest; } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs index afa7494..ba06ddd 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/PlayerLoopHelper.cs @@ -110,27 +110,30 @@ namespace Cysharp.Threading.Tasks #if UNITY_EDITOR EditorApplication.playModeStateChanged += (state) => { - if (state == PlayModeStateChange.EnteredEditMode || state == PlayModeStateChange.EnteredPlayMode) + if (state == PlayModeStateChange.EnteredEditMode || state == PlayModeStateChange.ExitingEditMode) { - return; - } + // run rest action before clear. + if (runner != null) + { + runner.Run(); + runner.Clear(); + } + if (lastRunner != null) + { + lastRunner.Run(); + lastRunner.Clear(); + } - if (runner != null) - { - runner.Clear(); - } - if (lastRunner != null) - { - lastRunner.Clear(); - } - - if (cq != null) - { - cq.Clear(); - } - if (lastCq != null) - { - lastCq.Clear(); + if (cq != null) + { + cq.Run(); + cq.Clear(); + } + if (lastCq != null) + { + lastCq.Run(); + lastCq.Clear(); + } } }; #endif diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index edbe43d..7fa0bf9 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -492,8 +492,33 @@ public class SandboxMain : MonoBehaviour Debug.Log("Current SyncContext:" + SynchronizationContext.Current.GetType().FullName); } + + async UniTask QuitCheck() + { + try + { + await UniTask.Delay(TimeSpan.FromMinutes(1), cancellationToken: quitSource.Token); + } + finally + { + Debug.Log("End QuitCheck async"); + } + } + + CancellationTokenSource quitSource = new CancellationTokenSource(); + async UniTaskVoid Start() { + Debug.Log("App Start"); + + Application.quitting += () => + { + Debug.Log("called quitting"); + quitSource.Cancel(); + }; + + QuitCheck().Forget(); + //Expression.Lambda>(null).Compile(true); //RunStandardTaskAsync();