public async Task <int> ExecuteWithProcessX() { var counter = 0; var(_, stdOutStream, stdErrStream) = ProcessX.GetDualAsyncEnumerable(FilePath, arguments: Args); var consumeStdOutTask = Task.Run(async() => { await foreach (var _ in stdOutStream) { counter++; } }); var consumeStdErrorTask = Task.Run(async() => { await foreach (var _ in stdErrStream) { counter++; } }); await Task.WhenAll(consumeStdOutTask, consumeStdErrorTask); return(counter); }
public async Task <(string, string)> ExecuteWithProcessX() { var(_, stdOutStream, stdErrStream) = ProcessX.GetDualAsyncEnumerable(FilePath, arguments: Args); var stdOutTask = stdOutStream.ToTask(); var stdErrTask = stdErrStream.ToTask(); await Task.WhenAll(stdOutTask, stdErrTask); return(string.Join(Environment.NewLine, stdOutTask.Result), string.Join(Environment.NewLine, stdErrTask.Result)); }
/// <inheritdoc /> public async ValueTask ExecuteAsync(string filePath) { if (!File.Exists(filePath)) { throw new FileNotFoundException(this.resourceService.GetCultureString(nameof(Resources.ExecutableFileNotFound)), filePath); } var(_, stdOut, stdError) = ProcessX.GetDualAsyncEnumerable(fileName: filePath, null, Directory.GetParent(filePath).FullName); var stdOutTask = Task.Run(async() => { await foreach (var item in stdOut) { this.logger.ZLogInformation(item); } }); var stdErrorTask = Task.Run(async() => { await foreach (var item in stdError) { this.logger.ZLogError(item); } }); try { await Task.WhenAll(stdOutTask, stdErrorTask).ConfigureAwait(false); } catch (ProcessErrorException processErrorException) { this.logger.ZLogError( processErrorException, "[{0}] {1}", DateTimeOffset.Now.LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture), this.resourceService.GetCultureString(nameof(Resources.FailedStartExecutableFile))); throw; } }