Skip to content

Instantly share code, notes, and snippets.

@mjvh80
Created October 18, 2013 09:56
Show Gist options
  • Select an option

  • Save mjvh80/7039284 to your computer and use it in GitHub Desktop.

Select an option

Save mjvh80/7039284 to your computer and use it in GitHub Desktop.
// Task.WaitAll(_sProcesses.Select(p => _ReadFromConsoleAsync(p)).ToArray());
private static async Task _ReadFromConsoleAsync(Process p)
{
// Starst two tasks, waiting for any to complete and restarting one that just completed for next line.
Task<String> stdoutTask, stderrTask;
using (var stdoutReader = p.StandardOutput)
using (var stderrReader = p.StandardError)
{
stdoutTask = stdoutReader.ReadLineAsync();
stderrTask = stderrReader.ReadLineAsync();
while (!p.HasExited)
{
var completedTask = await Task.WhenAny<String>(stdoutTask, stderrTask);
var line = await completedTask;
if (line == null) break; // EOP
if (completedTask == stdoutTask)
{
Console.WriteLine(line);
stdoutTask = stdoutReader.ReadLineAsync(); // restart
}
else
{
Console.Error.WriteLine(line);
stderrTask = stderrReader.ReadLineAsync();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment