Created
October 18, 2013 09:56
-
-
Save mjvh80/7039284 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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