Skip to content

Fix system.diagnostics.process.standardoutput code snippets #11515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@

public class Example
{
public static void Main()
{
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string eOut = null;
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
{ eOut += e.Data; });
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
public static void Main()
{
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string eOut = null;
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
{ eOut += e.Data; });
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();

// To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
p.BeginErrorReadLine();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
// To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
p.BeginErrorReadLine();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
Console.WriteLine($"\nError stream: {eOut}");
}
Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
Console.WriteLine($"\nError stream: {eOut}");
}
}
// The example displays the following output:
// The last 50 characters in the output stream are:
// ' 49,800.20%
// Line 500 of 500 written: 49,900.20%
// 'ritten: 99,80%
// Line 500 of 500 written: 100,00%
// '
//
// Error stream: Successfully wrote 500 lines.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@

public class Example2
{
public static void Main()
{
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
public static void Main()
{
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();

// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
}
Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
}
}
// The example displays the following output:
// Successfully wrote 500 lines.
//
// The last 50 characters in the output stream are:
// ' 49,800.20%
// Line 500 of 500 written: 49,900.20%
// 'ritten: 99,80%
// Line 500 of 500 written: 100,00%
// '
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System;
using System.IO;

public class Example3
{
public static void Main()
{
for (int ctr = 0; ctr < 500; ctr++)
Console.WriteLine($"Line {ctr + 1} of 500 written: {ctr + 1/500.0:P2}");
public static void Main()
{
for (int ctr = 0; ctr < 500; ctr++)
Console.WriteLine($"Line {ctr + 1} of 500 written: {(ctr + 1) / 500.0:P2}");

Console.Error.WriteLine("\nSuccessfully wrote 500 lines.\n");
}
Console.Error.WriteLine("\nSuccessfully wrote 500 lines.\n");
}
}
// The example displays the following output:
// The last 50 characters in the output stream are:
// ' 49,800.20%
// Line 500 of 500 written: 49,900.20%
//'
// Line 1 of 500 written: 0,20%
// Line 2 of 500 written: 0,40%
// Line 3 of 500 written: 0,60%
// ...
// Line 498 of 500 written: 99,60%
// Line 499 of 500 written: 99,80%
// Line 500 of 500 written: 100,00%
//
// Error stream: Successfully wrote 500 lines.
// Successfully wrote 500 lines.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ printfn $"The last 50 characters in the output stream are:\n'{output.Substring(o
printfn $"\nError stream: {eOut}"
// The example displays the following output:
// The last 50 characters in the output stream are:
// ' 49,800.20%
// Line 500 of 500 written: 49,900.20%
// 'ritten: 99,80%
// Line 500 of 500 written: 100,00%
// '
//
// Error stream: Successfully wrote 500 lines.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ p.WaitForExit()
printfn $"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'"
// The example displays the following output:
// Successfully wrote 500 lines.
//
// The last 50 characters in the output stream are:
// ' 49,800.20%
// Line 500 of 500 written: 49,900.20%
// 'ritten: 99,80%
// Line 500 of 500 written: 100,00%
// '
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ for i in 1. .. 500. do

eprintfn "Successfully wrote 500 lines.";
// The example displays the following output:
// The last 50 characters in the output stream are:
// ' 49,800.20%
// Line 500 of 500 written: 49,900.20%
//'
//
// Error stream: Successfully wrote 500 lines.
// Line 1 of 500 written: 0,20%
// Line 2 of 500 written: 0,40%
// Line 3 of 500 written: 0,60%
// ...
// Line 498 of 500 written: 99,60%
// Line 499 of 500 written: 99,80%
// Line 500 of 500 written: 100,00%
// Successfully wrote 500 lines.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
Public Module Example
Public Sub Main()
For ctr As Integer = 0 To 499
Console.WriteLine($"Line {ctr + 1} of 500 written: {ctr + 1/500.0:P2}")
Console.WriteLine($"Line {ctr + 1} of 500 written: {(ctr + 1) / 500.0:P2}")
Next

Console.Error.WriteLine($"{vbCrLf}Successfully wrote 500 lines.{vbCrLf}")
End Sub
End Module
' The example displays the following output:
' The last 50 characters in the output stream are:
' ' 49,800.20%
' Line 500 of 500 written: 49,900.20%
' Line 1 of 500 written 0,20%
' Line 2 of 500 written: 0,40%
' Line 3 of 500 written: 0,60%
' ...
' Line 498 of 500 written: 99,60%
' Line 499 of 500 written: 99,80%
' Line 500 of 500 written: 100,00%
'
'
' Error stream: Successfully wrote 500 lines.
' Successfully wrote 500 lines.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Public Module Example
End Module
' The example displays the following output:
' The last 50 characters in the output stream are:
' ' 49,800.20%
' Line 500 of 500 written: 49,900.20%
' 'ritten: 99,80%
' Line 500 of 500 written: 100,00%
' '
'
' Error stream: Successfully wrote 500 lines.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ End Module
' Successfully wrote 500 lines.
'
' The last 50 characters in the output stream are:
' ' 49,800.20%
' Line 500 of 500 written: 49,900.20%
' 'ritten: 99,80%
' Line 500 of 500 written: 100,00%
' '