Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to chain stdout to stdin for multiple child processes in .NET/C#?
    primarykey
    data
    text
    <p>I have a existing shell-like command line utility written in C# (Console) that I'm trying to add piping/redirection support to, so that a command like:</p> <blockquote> <p>MyShellPrompt> SomeCustomCommand | a.exe | b.exe | c.exe</p> </blockquote> <p>can be made that will create a,b, and c.exe and connect their stdin/stdouts for example as bash would. To be specific, my shell has shell-specific custom commands (interacts with custom hardware) whose output is written to a.exe's stdin, a.exe's stdout is piped to b.exe's stdin, and b.exe's stdout is piped to c.exe's stdin. c.exe's stdout is inherited by my shell program and so it just writes to the console. </p> <p>Answers such as <a href="https://stackoverflow.com/questions/1349543/redirecting-stdout-of-one-process-object-to-stdin-of-another">this one</a> show that you can redirect stdin and stdout, but you're not creating the sub-processes to reference opposite ends of the same pipe, which leaves you (the launching program) to "connect" the ends of the two pipes (output from one process to input of 2nd process) by grinding through the data, copying from the stdout of one app into the stdin of the next. This can get tedious in the general case, and doesn't seem like it should be necessary given that this can be done in C++ <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx" rel="nofollow noreferrer">Win32 example</a>, and <a href="https://stackoverflow.com/questions/5225810/is-it-possible-to-have-pipe-between-two-child-processes-created-by-same-parent">C/Linux</a>. But it appears that the .NET <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process_properties.aspx" rel="nofollow noreferrer">System.Diagnostics.Process</a> class does not provide for any kind of pipe assignment as the StandardInput and StandardOutput properties are read-only.</p> <p>Is there a way to do this in .NET? (Or was my choice of C# for my shell utility doomed (in this regard) from the start due to this lack of functionality (wouldn't be the first time...) :-) ). Ideally I would avoid P/Invoke as this breaks platform independence...</p> <p>If your answer is 'yes, you can do this by copying the data between the pipes' (as described above), then I'll just throw out there that (at least for me), the general case implementation has proven to be slightly more difficult when dealing with N EXEs, and unlimited (or at least large) amounts of data flowing through the chain. It appears that one has to use asynchronous I/O to hop between each of the N-1 pipe connections to keep them all going (I've not actually tried this yet...), or dedicate N-1 threads to each pipe connection (Does appear to work...). :(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload