0

I'm writing C# code that runs Git commands via ProcessStartInfo.

I am aware in terms of syntax that I can run commands from the console in parallel by adding a single & character between them. For example:

echo "Hello world!" & echo "Goodbye world!"

Because these commands run in parallel they're asynchronous, meaning their output order is random. The issue is that in my C# code I need to use the response from each command, and unfortunately there is no indicative output to tell me which output belongs to which command I ran.

Is there a way to tell which output came from which command?

For example, a way to echo specific text when the specific command has finished?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Clicksurfer
  • 126
  • 10
  • 1
    Start two separate `echo` processes instead; then you can add separate handlers for the output from each. – Aasmund Eldhuset Nov 15 '22 at 11:36
  • Does this answer your question? [c# Run multiple commands in cmd.exe simultaneously and write the output to a text file](https://stackoverflow.com/questions/38270912/c-sharp-run-multiple-commands-in-cmd-exe-simultaneously-and-write-the-output-to) – NineBerry Nov 19 '22 at 00:33

2 Answers2

2

Just as you can open multiple command windows as a user and type commands in them, you can run multiple commandline programs as separate processes from C# in parallel and so read their output separately.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
0
echo "Hello world!" >1st.out 2>1st.err & 
echo "Goodbye world!" >2nd.out 2>2nd.err &

Run each command by redirecting its standard out and error to different files, then the output of each commands are isolated from each other.

  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Nov 19 '22 at 00:03