C# 为什么我试着阅读时管子会挂起来?
我正在制作一个启动子进程并通过匿名管道进行通信的程序。当我从管道读取时,程序挂起在第一个C# 为什么我试着阅读时管子会挂起来?,c#,.net-core,pipe,C#,.net Core,Pipe,我正在制作一个启动子进程并通过匿名管道进行通信的程序。当我从管道读取时,程序挂起在第一个ReadLine()调用处,如以下代码方法所示: // Reads messages sent from module's process via anonymous pipe internal string[] ReadPipe() { try { Log.Verbose("Checking read pipe");
ReadLine()
调用处,如以下代码方法所示:
// Reads messages sent from module's process via anonymous pipe
internal string[] ReadPipe() {
try {
Log.Verbose("Checking read pipe");
// Check for sync message and return null if there is no message to receive
string pipeMessage = _pipeInReader.ReadLine(); // HANGS ON THIS LINE
if(pipeMessage == null || !pipeMessage.StartsWith("SYNC")) {
Log.Verbose("No message found in pipe");
return null;
}
// Return array of message lines
Log.Verbose("Received message from module {ModuleName}", _fileInfo.Name);
List<string> pipeMessageLines = new();
do {
pipeMessage = _pipeInReader.ReadLine();
pipeMessageLines.Add(pipeMessage);
Log.Debug(pipeMessage);
} while(pipeMessage != null && !pipeMessage.StartsWith("END"));
return pipeMessageLines.ToArray();
} catch(Exception e) {
Log.Error(e.ToString());
return null;
}
}
为什么它挂在ReadLine()
行上?
提前谢谢 没有合适的答案,就不可能确定。但是,代码的一个明显问题是,当您写入\u pipeOutWriter
对象时,您没有调用Flush()
。假设这是一个文本编写器
,默认情况下,它将缓冲数据直到内部缓冲区已满,并且在此之前不会向底层流
发送任何内容
通过调用Flush()
,您可以强制它刷新其内部缓冲区并立即发送数据
如果这还不能解决您的问题,请改进它,确保提供详细信息,以及您迄今为止尝试解决问题的其他细节,以及您需要帮助的具体内容
// Sends a message to module's process via anonymous pipe
public static void WritePipe(string message) {
try {
Log.Verbose("Sending \"{Message}\" to kit pipe", message);
// Send sync message and wait for module process to receive it
Log.Verbose("Waiting for pipe drain");
_pipeOutWriter.Write("SYNC");
_pipeOut.WaitForPipeDrain();
// Send the specified message
Log.Verbose("Pipe drained. Sending message");
_pipeOutWriter.Write(message);
_pipeOutWriter.Write("END");
} catch(Exception e) {
Log.Error(e.ToString());
}
}