I have a process that needs to launch a child process and communicate with it through standard input and output. The child process should be able to automatically terminate itself; however, I am having trouble closing the streams properly.
Here is the relevant code from the child / "client" process:
// This is started in a separate thread
private void watchStandardInput() {
string line = null;
do {
try {
line = Console.ReadLine();
} catch (IOException) {
return;
}
lock (inputWatcher) {
switch (line) {
// process command
}
}
} while (line != null);
}
// This is called from the main thread
private void updateStatus(string statusMessage) {
lock (inputWatcher) {
Console.WriteLine(statusMessage);
}
}
And here's the "server" code:
// This is in the main thread
using (StreamReader sr = process.StandardOutput) {
while (!process.HasExited) {
processOutput(sr.ReadLine());
}
}
// Finally, commands are sent in a separate thread.
Now for the problem I am having: When the child process is supposed to be exiting, the server stays at sr.ReadLine() and the client stays at Console.ReadLine(). What am I doing wrong?
Console.WriteLine("Hey I'm closing")but that's ugly and it would break existing code.Thread.Abort()on it. I'm well aware that the app isn't closing because that thread is sitting around waiting for input - my question is how can I get it to close?