Beispiel #1
0
        public void BeginOperation(CancellationToken cancellationToken, Action <object?> callback, object?state)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // Don't register if already completed, we would immediately unregistered in ObserveCancellation
            if (cancellationToken.CanBeCanceled && !IsCompleted)
            {
#if DEBUG
                var previousAwaitableState = _awaitableState;
#endif

                _cancellationTokenRegistration = cancellationToken.UnsafeRegister(callback, state);

                // If we get back the default CancellationTokenRegistration then it means the
                // callback synchronously and we can throw inline. This is safe because we haven't changed
                // the state of the awaitable as yet.
                if (_cancellationTokenRegistration == default(CancellationTokenRegistration))
                {
#if DEBUG
                    Debug.Assert(previousAwaitableState == _awaitableState, "The awaitable state changed!");
#endif

                    cancellationToken.ThrowIfCancellationRequested();
                }

#if (NETSTANDARD2_0 || NETFRAMEWORK)
                _cancellationToken = cancellationToken;
#endif
            }

            _awaitableState |= AwaitableState.Running;
        }
        public bool ObserveCancellation()
        {
            bool isCanceled = (_awaitableState & AwaitableState.Canceled) == AwaitableState.Canceled;

            _awaitableState &= ~(AwaitableState.Canceled | AwaitableState.Running);

            return(isCanceled);
        }
        public void SetUncompleted()
        {
            Debug.Assert(_completion == null);
            Debug.Assert(_completionState == null);
            Debug.Assert(_schedulingContext == null);

            _awaitableState &= ~AwaitableState.Completed;
        }
        public void SetUncompleted()
        {
            Debug.Assert(_completion == null);
            Debug.Assert(_completionState == null);
            Debug.Assert(_synchronizationContext == null);
            Debug.Assert(_executionContext == null);

            _awaitableState &= ~AwaitableState.Completed;
        }
        public void Reset()
        {
            _completion             = null;
            _completionState        = null;
            _synchronizationContext = null;
            _executionContext       = null;

            _awaitableState &= ~AwaitableState.Completed;
        }
        public bool ObserveCancellation()
        {
            bool isCanceled = (_awaitableState & AwaitableState.Canceled) == AwaitableState.Canceled;

            _awaitableState &= ~(AwaitableState.Canceled | AwaitableState.Running);

            _cancellationToken.ThrowIfCancellationRequested();

            return(isCanceled);
        }
 public PipeAwaitable(bool completed, bool useSynchronizationContext)
 {
     _awaitableState = (completed ? AwaitableState.Completed : AwaitableState.None) |
                       (useSynchronizationContext ? AwaitableState.UseSynchronizationContext : AwaitableState.None);
     _completion                    = null;
     _completionState               = null;
     _cancellationToken             = CancellationToken.None;
     _cancellationTokenRegistration = default;
     _synchronizationContext        = null;
     _executionContext              = null;
 }
        public PipeAwaitable(bool completed, bool useSynchronizationContext)
        {
            _awaitableState = (completed ? AwaitableState.Completed : AwaitableState.None) |
                              (useSynchronizationContext ? AwaitableState.UseSynchronizationContext : AwaitableState.None);
            _completion      = null;
            _completionState = null;
            _cancellationTokenRegistration = default;
            _schedulingContext             = null;
#if (NETSTANDARD2_0 || NETFRAMEWORK)
            _cancellationToken = CancellationToken.None;
#endif
        }
        public void BeginOperation(CancellationToken cancellationToken, Action <object?> callback, object?state)
        {
            cancellationToken.ThrowIfCancellationRequested();

            _awaitableState |= AwaitableState.Running;

            // Don't register if already completed, we would immediately unregistered in ObserveCancellation
            if (cancellationToken.CanBeCanceled && !IsCompleted)
            {
#if NETSTANDARD2_0
                _cancellationToken = cancellationToken;
#endif
                _cancellationTokenRegistration = cancellationToken.UnsafeRegister(callback, state);
            }
        }
        public CancellationTokenRegistration BeginOperation(CancellationToken cancellationToken, Action <object> callback, object state)
        {
            _awaitableState |= AwaitableState.Running;

            CancellationTokenRegistration oldRegistration = default;

            if (!cancellationToken.Equals(_cancellationToken))
            {
                oldRegistration    = _cancellationTokenRegistration;
                _cancellationToken = cancellationToken;
                if (_cancellationToken.CanBeCanceled)
                {
                    _cancellationToken.ThrowIfCancellationRequested();
                    _cancellationTokenRegistration = _cancellationToken.UnsafeRegister(callback, state);
                }
            }
            return(oldRegistration);
        }
        public void Complete(out CompletionData completionData)
        {
            ExtractCompletion(out completionData);

            _awaitableState |= AwaitableState.Completed;
        }