ToCancellationToken(linkeToken)
parent
3ed6e28a00
commit
c06e45d0bb
|
@ -18,11 +18,32 @@ namespace Cysharp.Threading.Tasks
|
|||
return cts.Token;
|
||||
}
|
||||
|
||||
public static CancellationToken ToCancellationToken<T>(this UniTask<T> task)
|
||||
public static CancellationToken ToCancellationToken(this UniTask task, CancellationToken linkToken)
|
||||
{
|
||||
if (linkToken.IsCancellationRequested)
|
||||
{
|
||||
return linkToken;
|
||||
}
|
||||
|
||||
if (!linkToken.CanBeCanceled)
|
||||
{
|
||||
return ToCancellationToken(task);
|
||||
}
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
ToCancellationTokenCore(task, cts).Forget();
|
||||
return cts.Token;
|
||||
|
||||
return CancellationTokenSource.CreateLinkedTokenSource(linkToken).Token;
|
||||
}
|
||||
|
||||
public static CancellationToken ToCancellationToken<T>(this UniTask<T> task)
|
||||
{
|
||||
return ToCancellationToken(task.AsUniTask());
|
||||
}
|
||||
|
||||
public static CancellationToken ToCancellationToken<T>(this UniTask<T> task, CancellationToken linkToken)
|
||||
{
|
||||
return ToCancellationToken(task.AsUniTask(), linkToken);
|
||||
}
|
||||
|
||||
static async UniTaskVoid ToCancellationTokenCore(UniTask task, CancellationTokenSource cts)
|
||||
|
|
|
@ -434,16 +434,31 @@ public class SandboxMain : MonoBehaviour
|
|||
throw new Exception("yeah");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Start()
|
||||
|
||||
async UniTaskVoid Start()
|
||||
{
|
||||
_ = Foo(); // unhandled.
|
||||
Go();
|
||||
//_ = Foo(); // unhandled.
|
||||
//Go();
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
okButton.onClick.AddListener(() =>
|
||||
{
|
||||
cts.Cancel();
|
||||
});
|
||||
|
||||
|
||||
UnityEngine.Debug.Log("Start:" + PlayerLoopInfo.CurrentLoopType);
|
||||
|
||||
var token = UniTask.Delay(TimeSpan.FromSeconds(3), DelayType.Realtime).ToCancellationToken(cts.Token);
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
UnityEngine.Debug.Log("in loop");
|
||||
await UniTask.Yield();
|
||||
}
|
||||
UnityEngine.Debug.Log("end");
|
||||
|
||||
|
||||
// this.GetCancellationTokenOnDestroy()
|
||||
|
||||
|
|
|
@ -190,7 +190,7 @@ namespace Cysharp.Threading.TasksTests
|
|||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
await UniTask.DelayRealtime(TimeSpan.FromSeconds(2));
|
||||
await UniTask.Delay(TimeSpan.FromSeconds(2), DelayType.Realtime);
|
||||
|
||||
var elapsed = DateTimeOffset.UtcNow - now;
|
||||
|
||||
|
|
Loading…
Reference in New Issue