Fix WaitWhile(bool cancelImmediately = true) does not work correctly

, and when cancelImmediately = true and canceled task remove Trakcking immediately
pull/623/head
neuecc 2024-09-24 17:12:50 +09:00
parent f7b3c2fbe1
commit 75119acb50
10 changed files with 253 additions and 17 deletions

View File

@ -205,6 +205,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -404,7 +408,13 @@ namespace Cysharp.Threading.Tasks
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}

View File

@ -259,6 +259,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -366,6 +370,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -475,6 +483,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -604,6 +616,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -750,6 +766,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -878,6 +898,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -1004,6 +1028,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}

View File

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Threading;
using Cysharp.Threading.Tasks.Internal;
@ -102,6 +103,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -193,6 +198,7 @@ namespace Cysharp.Threading.Tasks
result.predicate = predicate;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -223,6 +229,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -343,6 +353,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -459,6 +473,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -598,6 +616,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}

View File

@ -162,6 +162,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}

View File

@ -97,6 +97,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}

View File

@ -158,6 +158,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -384,6 +388,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -615,6 +623,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -847,6 +859,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}
@ -1095,6 +1111,10 @@ namespace Cysharp.Threading.Tasks
{
TryReturn();
}
else
{
TaskTracker.RemoveTracking(this);
}
}
}

View File

@ -20,8 +20,11 @@ using System.IO;
using System.Linq.Expressions;
// using DG.Tweening;
public struct MyJob : IJob
{
public int loopCount;

View File

@ -38,12 +38,11 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 11
serializedVersion: 12
m_GIWorkflowMode: 1
m_GISettings:
serializedVersion: 2
@ -98,13 +97,14 @@ LightmapSettings:
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1
m_LightingSettings: {fileID: 4890085278179872738, guid: 814185d368762ed45a2298d112780689,
type: 2}
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
@ -117,7 +117,9 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
@ -150,10 +152,10 @@ RectTransform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1584557232}
m_Father: {fileID: 1556045508}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
@ -174,6 +176,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
@ -218,6 +221,8 @@ MonoBehaviour:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
@ -270,9 +275,17 @@ Camera:
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
@ -306,12 +319,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 518730348}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 488, y: 418, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &519420028
GameObject:
@ -352,9 +366,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: f0bc6c75abb2e0b47a25aa49bfd488ed, type: 3}
m_Name:
m_EditorClassIdentifier:
camera: {fileID: 518730349}
mycamera: {fileID: 0}
okButton: {fileID: 16537672}
cancelButton: {fileID: 628393011}
RP1:
latestValue: 0
text: {fileID: 2101290655}
button: {fileID: 0}
--- !u!20 &519420031
@ -371,9 +387,17 @@ Camera:
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
@ -407,12 +431,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &628393009
GameObject:
@ -443,10 +468,10 @@ RectTransform:
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 865871445}
m_Father: {fileID: 1556045508}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
@ -467,6 +492,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
@ -511,6 +537,8 @@ MonoBehaviour:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
@ -560,9 +588,9 @@ RectTransform:
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 628393010}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
@ -584,6 +612,8 @@ MonoBehaviour:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
@ -639,6 +669,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4f231c4fb786f3946a6b90b886c48677, type: 3}
m_Name:
m_EditorClassIdentifier:
m_SendPointerHoverToParent: 1
m_HorizontalAxis: Horizontal
m_VerticalAxis: Vertical
m_SubmitButton: Submit
@ -668,12 +699,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 872009839}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1556045504
GameObject:
@ -687,6 +719,7 @@ GameObject:
- component: {fileID: 1556045507}
- component: {fileID: 1556045506}
- component: {fileID: 1556045505}
- component: {fileID: 1556045509}
m_Layer: 0
m_Name: Canvas
m_TagString: Untagged
@ -733,6 +766,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!223 &1556045507
Canvas:
m_ObjectHideFlags: 0
@ -750,7 +784,9 @@ Canvas:
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_VertexColorAlwaysGammaSpace: 0
m_AdditionalShaderChannelsFlag: 0
m_UpdateRectTransformForStandalone: 0
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
@ -764,18 +800,30 @@ RectTransform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 16537671}
- {fileID: 628393010}
- {fileID: 2101290654}
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!114 &1556045509
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1556045504}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a478e5f6126dc184ca902adfb35401b4, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1584557231
GameObject:
m_ObjectHideFlags: 0
@ -804,9 +852,9 @@ RectTransform:
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 16537671}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
@ -828,6 +876,8 @@ MonoBehaviour:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
@ -883,9 +933,9 @@ RectTransform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1556045508}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
@ -907,6 +957,8 @@ MonoBehaviour:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
@ -932,3 +984,11 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2101290653}
m_CullTransparentMesh: 0
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 519420032}
- {fileID: 518730350}
- {fileID: 872009842}
- {fileID: 1556045508}

View File

@ -0,0 +1,74 @@
using Cysharp.Threading.Tasks;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
// https://github.com/Cysharp/UniTask/issues/617
public class WaitWhileTest : MonoBehaviour
{
private const float c_CallInterval = 0.3f;
private float m_JustBeforeCallTime;
private TaskObj m_TestObj;
// Start is called before the first frame update
void Start()
{
m_JustBeforeCallTime = Time.unscaledTime;
m_TestObj = new TaskObj();
// m_TestObj.Test(CancellationToken.None).Forget();
}
// Update is called once per frame
void Update()
{
if (Time.unscaledTime - m_JustBeforeCallTime > c_CallInterval)
{
m_JustBeforeCallTime = Time.unscaledTime;
m_TestObj.Test(CancellationToken.None).Forget();
}
}
}
public class TaskObj
{
private CancellationTokenSource m_CancelTokenSource;
private const float c_FinishElapsedTime = 0.1f;
private float m_StartTime;
public async UniTask Test(CancellationToken token)
{
try
{
CancelAndDisposeTokenSource();
m_CancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token);
m_StartTime = Time.unscaledTime;
await UniTask.WaitWhile(IsContinued, cancellationToken: m_CancelTokenSource.Token, cancelImmediately: true);
Debug.Log("Task Finished");
}
catch (OperationCanceledException)
{
Debug.LogWarning("Task Canceled");
}
finally
{
CancelAndDisposeTokenSource();
}
}
private void CancelAndDisposeTokenSource()
{
m_CancelTokenSource?.Cancel();
m_CancelTokenSource?.Dispose();
m_CancelTokenSource = null;
}
private bool IsContinued()
{
return Time.unscaledTime - m_StartTime > c_FinishElapsedTime;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a478e5f6126dc184ca902adfb35401b4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: