Add IDisposable.AddTo(cancellationToken)

pull/92/head
neuecc 2020-06-11 23:31:55 +09:00
parent 1b553f67b0
commit 9c86cfb508
2 changed files with 19 additions and 1 deletions

View File

@ -9,6 +9,7 @@ namespace Cysharp.Threading.Tasks
public static class CancellationTokenExtensions
{
static readonly Action<object> cancellationTokenCallback = Callback;
static readonly Action<object> disposeCallback = DisposeCallback;
public static (UniTask, CancellationTokenRegistration) ToUniTask(this CancellationToken cancellationToken)
{
@ -75,6 +76,17 @@ namespace Cysharp.Threading.Tasks
}
}
}
public static CancellationTokenRegistration AddTo(this IDisposable disposable, CancellationToken cancellationToken)
{
return cancellationToken.RegisterWithoutCaptureExecutionContext(disposeCallback, disposable);
}
static void DisposeCallback(object state)
{
var d = (IDisposable)state;
d.Dispose();
}
}
public struct CancellationTokenAwaitable

View File

@ -429,7 +429,9 @@ public class SandboxMain : MonoBehaviour
void Start()
{
PlayerLoopInfo.Inject();
UnityEngine.Debug.Log("Start:" + PlayerLoopInfo.CurrentLoopType);
//PlayerLoopInfo.Inject();
//_ = AsyncFixedUpdate();
//StartCoroutine(CoroutineFixedUpdate());
@ -438,8 +440,11 @@ public class SandboxMain : MonoBehaviour
// Application.logMessageReceived += Application_logMessageReceived;
// var rp = new AsyncReactiveProperty<int>();
// rp.AddTo(this.GetCancellationTokenOnDestroy());
@ -969,6 +974,7 @@ public class PlayerLoopInfo
public static Type CurrentLoopType { get; private set; }
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
public static void Inject()
{
var system = PlayerLoop.GetCurrentPlayerLoop();