Skip to content
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

Example code incorrectly calculates percentage #11121

Open
jcowin-sigmadzn opened this issue Mar 26, 2025 · 2 comments
Open

Example code incorrectly calculates percentage #11121

jcowin-sigmadzn opened this issue Mar 26, 2025 · 2 comments
Labels
area-System.Diagnostics.Process help wanted Good for community contributors to help [up-for-grabs]
Milestone

Comments

@jcowin-sigmadzn
Copy link

Type of issue

Code doesn't work

Description

If the intent was to show the progress percentage, parentheses are needed around (ctr + 1):

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

Example Output

Line 498 of 500 written: 99.60%
Line 499 of 500 written: 99.80%
Line 500 of 500 written: 100.00%

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=net-8.0#remarks

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Diagnostics/Process.xml

Document Version Independent Id

981e4b00-7db9-eeab-ce84-6f23ecde07a1

Platform Id

8ec5d3e5-25ef-3c4a-5a94-e2d67ac91358

Article author

@dotnet-bot

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 26, 2025
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Mar 26, 2025
@jozkee jozkee added area-System.Diagnostics.Process and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Mar 28, 2025
@jozkee jozkee added this to the Backlog milestone Mar 28, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Mar 28, 2025
@jozkee jozkee added untriaged New issue has not been triaged by the area owner help wanted Good for community contributors to help [up-for-grabs] and removed untriaged New issue has not been triaged by the area owner labels Mar 28, 2025
@jozkee
Copy link
Member

jozkee commented Mar 28, 2025

Thanks, would you like to send a PR to fix it? Here are the paths to the code snippets:

:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs":::
:::code language="fsharp" source="~/snippets/fsharp/System.Diagnostics/Process/StandardOutput/write500lines.fs":::
:::code language="vb" source="~/snippets/visualbasic/api/system.diagnostics/process/standardoutput/write500lines.vb":::
The following example shows how to read from a redirected stream and wait for the child process to exit. The example avoids a deadlock condition by calling `p.StandardOutput.ReadToEnd` before `p.WaitForExit`. A deadlock condition can result if the parent process calls `p.WaitForExit` before `p.StandardOutput.ReadToEnd` and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full <xref:System.Diagnostics.Process.StandardOutput%2A> stream.
:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-sync.cs":::
:::code language="fsharp" source="~/snippets/fsharp/System.Diagnostics/Process/StandardOutput/stdoutput-sync.fs":::
:::code language="vb" source="~/snippets/visualbasic/api/system.diagnostics/process/standardoutput/stdoutput-sync.vb":::
There is a similar issue when you read all text from both the standard output and standard error streams. The following example performs a read operation on both streams. It avoids the deadlock condition by performing asynchronous read operations on the <xref:System.Diagnostics.Process.StandardError%2A> stream. A deadlock condition results if the parent process calls `p.StandardOutput.ReadToEnd` followed by `p.StandardError.ReadToEnd` and the child process writes enough text to fill its error stream. The parent process would wait indefinitely for the child process to close its <xref:System.Diagnostics.Process.StandardOutput%2A> stream. The child process would wait indefinitely for the parent to read from the full <xref:System.Diagnostics.Process.StandardError%2A> stream.
:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-async.cs":::
:::code language="fsharp" source="~/snippets/fsharp/System.Diagnostics/Process/StandardOutput/stdoutput-async.fs":::
:::code language="vb" source="~/snippets/visualbasic/api/system.diagnostics/process/standardoutput/stdoutput-async.vb":::

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Diagnostics.Process help wanted Good for community contributors to help [up-for-grabs]
Projects
None yet
Development

No branches or pull requests

2 participants