JobHandle.WaitAsync accepts CancellationToken
parent
7cce0f48e5
commit
8b7f832c0f
|
@ -9,10 +9,11 @@ namespace Cysharp.Threading.Tasks
|
|||
{
|
||||
public static partial class UnityAsyncExtensions
|
||||
{
|
||||
public static async UniTask WaitAsync(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
|
||||
public static async UniTask WaitAsync(this JobHandle jobHandle, PlayerLoopTiming waitTiming, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await UniTask.Yield(waitTiming);
|
||||
jobHandle.Complete();
|
||||
cancellationToken.ThrowIfCancellationRequested(); // call cancel after Complete.
|
||||
}
|
||||
|
||||
public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle)
|
||||
|
@ -29,6 +30,8 @@ namespace Cysharp.Threading.Tasks
|
|||
return new UniTask(handler, token).GetAwaiter();
|
||||
}
|
||||
|
||||
// can not pass CancellationToken because can't handle JobHandle's Complete and NativeArray.Dispose.
|
||||
|
||||
public static UniTask ToUniTask(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
|
||||
{
|
||||
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
||||
|
|
|
@ -193,6 +193,8 @@ public class SandboxMain : MonoBehaviour
|
|||
async UniTask RunJobAsync()
|
||||
{
|
||||
var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
||||
try
|
||||
{
|
||||
JobHandle.ScheduleBatchedJobs();
|
||||
|
||||
var scheduled = job.Schedule();
|
||||
|
@ -200,9 +202,12 @@ public class SandboxMain : MonoBehaviour
|
|||
UnityEngine.Debug.Log("OK");
|
||||
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
||||
UnityEngine.Debug.Log("OK2");
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
job.inOut.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async UniTaskVoid Update2()
|
||||
|
|
Loading…
Reference in New Issue