| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Threading;
- namespace ETModel
- {
- public static class CancellationTokenExtensions
- {
- private static readonly Action<object> cancellationTokenCallback = Callback;
- public static (ETTask, CancellationTokenRegistration) ToETTask(this CancellationToken cts)
- {
- if (cts.IsCancellationRequested)
- {
- return (ETTask.FromCanceled(cts), default);
- }
- var promise = new ETTaskCompletionSource<AsyncUnit>();
- return (promise.Task, cts.RegisterWithoutCaptureExecutionContext(cancellationTokenCallback, promise));
- }
- private static void Callback(object state)
- {
- var promise = (ETTaskCompletionSource<AsyncUnit>) state;
- promise.TrySetResult(AsyncUnit.Default);
- }
- public static CancellationTokenRegistration RegisterWithoutCaptureExecutionContext(this CancellationToken cancellationToken, Action callback)
- {
- bool restoreFlow = false;
- if (!ExecutionContext.IsFlowSuppressed())
- {
- ExecutionContext.SuppressFlow();
- restoreFlow = true;
- }
- try
- {
- return cancellationToken.Register(callback, false);
- }
- finally
- {
- if (restoreFlow)
- {
- ExecutionContext.RestoreFlow();
- }
- }
- }
- public static CancellationTokenRegistration RegisterWithoutCaptureExecutionContext(this CancellationToken cancellationToken,
- Action<object> callback, object state)
- {
- bool restoreFlow = false;
- if (!ExecutionContext.IsFlowSuppressed())
- {
- ExecutionContext.SuppressFlow();
- restoreFlow = true;
- }
- try
- {
- return cancellationToken.Register(callback, state, false);
- }
- finally
- {
- if (restoreFlow)
- {
- ExecutionContext.RestoreFlow();
- }
- }
- }
- }
- }
|