Esempio n. 1
0
 static protected void ThrowProgressException(int skipFrames)
 {
     throw new InvalidOperationException("Progress is disabled. Remove PROTO_PROMISE_PROGRESS_DISABLE from your compiler symbols to enable progress reports.", Internal.GetFormattedStacktrace(skipFrames + 1));
 }
 /// <summary>
 /// Release all resources used by this <see cref="CancelationSource"/>. This instance will no longer be valid.
 /// </summary>
 public void Dispose()
 {
     if (!IsValid)
     {
         throw new InvalidOperationException("CancelationSource.Dispose: source is not valid.", Internal.GetFormattedStacktrace(1));
     }
     _ref.Dispose();
 }
 static partial void ValidateThreadAccess(int skipFrames)
 {
     Internal.ValidateThreadAccess(skipFrames + 1);
 }
 /// <summary>
 /// Communicates a request for cancelation with the provided reason, and invokes all callbacks that are registered to any associated <see cref="CancelationToken"/>.
 /// </summary>
 public void Cancel <TCancel>(TCancel reason)
 {
     if (!IsValid)
     {
         throw new InvalidOperationException("CancelationSource.Cancel: source is not valid.", Internal.GetFormattedStacktrace(1));
     }
     if (_ref.IsCanceled)
     {
         throw new InvalidOperationException("CancelationSource.Cancel: source was already canceled.", Internal.GetFormattedStacktrace(1));
     }
     _ref.SetCanceled(ref reason);
 }
 static partial void ValidateArgument(object arg, string argName, int skipFrames)
 {
     Internal.ValidateArgument(arg, argName, skipFrames + 1);
 }
 /// <summary>
 /// Release this instance. Allows resources to be released when the associated <see cref="CancelationSource"/> is disposed (if <see cref="Release"/> has been called for all <see cref="Retain"/> calls).
 /// <para/>This should always be paired with a call to <see cref="Retain"/>
 /// </summary>
 public void Release()
 {
     if (!CanBeCanceled)
     {
         throw new InvalidOperationException("CancelationToken.Release: token cannot be canceled.", Internal.GetFormattedStacktrace(1));
     }
     _ref.Release();
 }
        /// <summary>
        /// Capture a value and register a delegate that will be invoked with the captured value and the cancelation reason when this <see cref="CancelationToken"/> is canceled.
        /// If this is already canceled, the callback will be invoked immediately.
        /// </summary>
        /// <param name="callback">The delegate to be executed when the <see cref="CancelationToken"/> is canceled.</param>
        /// <returns>The <see cref="CancelationRegistration"/> instance that can be used to unregister the callback.</returns>
        public CancelationRegistration Register <TCapture>(TCapture captureValue, Action <TCapture, ReasonContainer> callback)
        {
            if (!CanBeCanceled)
            {
                throw new InvalidOperationException("CancelationToken.Register: token cannot be canceled.", Internal.GetFormattedStacktrace(1));
            }
            ValidateArgument(callback, "callback", 1);
            if (_ref.IsCanceled)
            {
                callback.Invoke(captureValue, new ReasonContainer(_ref.ValueContainer));
                return(default(CancelationRegistration));
            }
            var cancelDelegate = Internal.CancelDelegate <Internal.CancelDelegateToken <TCapture> > .GetOrCreate();

            cancelDelegate.canceler = new Internal.CancelDelegateToken <TCapture>(ref captureValue, callback);
            return(new CancelationRegistration(_ref, cancelDelegate));
        }
 /// <summary>
 /// Try to get the cancelation value casted to <typeparamref name="T"/>.
 /// Returns true if successful, false otherwise.
 /// </summary>
 public bool TryGetCancelationValueAs <T>(out T value)
 {
     if (!IsCancelationRequested)
     {
         throw new InvalidOperationException("CancelationToken.CancelationValue: token has not been canceled.", Internal.GetFormattedStacktrace(1));
     }
     return(Internal.TryConvert(_ref.ValueContainer, out value));
 }