mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-22 07:40:12 +00:00
JobHandle.WaitAsync accepts CancellationToken
This commit is contained in:
@@ -9,10 +9,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static partial class UnityAsyncExtensions
|
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);
|
await UniTask.Yield(waitTiming);
|
||||||
jobHandle.Complete();
|
jobHandle.Complete();
|
||||||
|
cancellationToken.ThrowIfCancellationRequested(); // call cancel after Complete.
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle)
|
public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle)
|
||||||
@@ -28,7 +29,9 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
return new UniTask(handler, token).GetAwaiter();
|
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)
|
public static UniTask ToUniTask(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
|
||||||
{
|
{
|
||||||
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
||||||
|
|||||||
@@ -193,15 +193,20 @@ public class SandboxMain : MonoBehaviour
|
|||||||
async UniTask RunJobAsync()
|
async UniTask RunJobAsync()
|
||||||
{
|
{
|
||||||
var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
||||||
JobHandle.ScheduleBatchedJobs();
|
try
|
||||||
|
{
|
||||||
|
JobHandle.ScheduleBatchedJobs();
|
||||||
|
|
||||||
var scheduled = job.Schedule();
|
var scheduled = job.Schedule();
|
||||||
|
|
||||||
UnityEngine.Debug.Log("OK");
|
UnityEngine.Debug.Log("OK");
|
||||||
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
||||||
UnityEngine.Debug.Log("OK2");
|
UnityEngine.Debug.Log("OK2");
|
||||||
|
}
|
||||||
job.inOut.Dispose();
|
finally
|
||||||
|
{
|
||||||
|
job.inOut.Dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user