public ThrowIfCancellationRequested ( ) : void | ||
return | void |
/// <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 }
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); }