ThrowIfCancellationRequested() public method

Throws an OperationCanceledException if cancellation has been requested.
Cancellation has been requested.
public ThrowIfCancellationRequested ( ) : void
return void
Ejemplo n.º 1
0
        /// <inheritdoc/>
        public void Run(CancellationToken cancellationToken = default(CancellationToken), ICredentialProvider credentialProvider = null, IProgress<TaskSnapshot> progress = null)
        {
            cancellationToken.ThrowIfCancellationRequested();
            CancellationToken = cancellationToken;
            _progress = progress;
            CredentialProvider = credentialProvider;

            State = TaskState.Started;

            try
            {
                // Run task with privileges of original user if possible
                if (_originalIdentity != null)
                {
                    using (_originalIdentity.Impersonate())
                        Execute();
                }
                else Execute();
            }
                #region Error handling
            catch (OperationCanceledException)
            {
                State = TaskState.Canceled;
                throw;
            }
            catch (IOException)
            {
                State = TaskState.IOError;
                throw;
            }
            catch (UnauthorizedAccessException)
            {
                State = TaskState.IOError;
                throw;
            }
            catch (WebException)
            {
                State = TaskState.WebError;
                throw;
            }
            #endregion
        }
Ejemplo n.º 2
0
        public static void CopyToEx([NotNull] this Stream source, [NotNull] Stream destination, int bufferSize = 4096, CancellationToken cancellationToken = default(CancellationToken), Tasks.IProgress<long> progress = null)
        {
            #region Sanity checks
            if (source == null) throw new ArgumentNullException(nameof(source));
            if (destination == null) throw new ArgumentNullException(nameof(destination));
            #endregion

            if (source.CanSeek) source.Position = 0;

            var buffer = new byte[bufferSize];
            int read;
            long sum = 0;
            do
            {
                cancellationToken.ThrowIfCancellationRequested();
                sum += read = source.Read(buffer, 0, buffer.Length);
                destination.Write(buffer, 0, read);
                progress?.Report(sum);
            } while (read != 0);
        }