/// <summary> /// Creates a new backtracking solver run. /// </summary> /// <param name="requirements">The top-level requirements the solver should try to meet.</param> /// <param name="cancellationToken">Used to signal when the user wishes to cancel the solver run.</param> /// <param name="candidateProvider">Generates <see cref="SelectionCandidate"/>s for the solver to choose among.</param> public Pass([NotNull] Requirements requirements, CancellationToken cancellationToken, [NotNull] SelectionCandidateProvider candidateProvider) { #region Sanity checks if (requirements == null) throw new ArgumentNullException(nameof(requirements)); #endregion _cancellationToken = cancellationToken; _candidateProvider = candidateProvider; _topLevelRequirements = requirements; Selections.InterfaceUri = requirements.InterfaceUri; Selections.Command = requirements.Command; }
/// <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 }
/// <summary> /// Writes the entire content of a stream to a file. /// </summary> /// <param name="stream">The stream to read from.</param> /// <param name="path">The path of the file to write.</param> /// <param name="bufferSize">The size of the buffer to use for copying in bytes.</param> /// <param name="cancellationToken">Used to signal when the user wishes to cancel the copy process.</param> /// <param name="progress">Used to report back the number of bytes that have been copied so far. Callbacks are rate limited to once every 250ms.</param> public static void CopyToFile([NotNull] this Stream stream, [NotNull, Localizable(false)] string path, int bufferSize = 4096, CancellationToken cancellationToken = default(CancellationToken), Tasks.IProgress<long> progress = null) { #region Sanity checks if (stream == null) throw new ArgumentNullException(nameof(stream)); if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path)); #endregion using (var fileStream = File.Create(path)) stream.CopyToEx(fileStream, bufferSize, cancellationToken, progress); }
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); }