我有一个进程需要启动一个子进程,并通过标准的输入和输出与它进行通信。子进程应该能够自动终止自己;但是,我无法正确关闭流。
下面是来自child/“client”进程的相关代码:
// 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);
}
}
下面是“服务器”代码:
// 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.
现在,对于我遇到的问题:当子进程应该退出时,服务器将保持在
sr.ReadLine()
客户停留在
Console.ReadLine()
. 我做错什么了?