在同一管道上运行的流的资源处理
首先,我写入 NamedPipeClientStream,然后从中读取。这基本上有效。但我没有正确处理 StreamReader 和 StreamWriter 的资源。
方法 1
using (StreamWriter sw = new StreamWriter(pipeStream))
{
// ...
using (StreamReader sr = new StreamReader(pipeStream))
{
// ...
}
}
处理 sw
失败,因为流已经关闭。
方法2
using (StreamWriter sw = new StreamWriter(pipeStream))
{
// ...
}
using (StreamReader sr = new StreamReader(pipeStream))
{
// ...
}
现在另一个进程中的管道服务器陷入困境,因为管道连接过早关闭。
方法3
在另一个有关文件流的问题中,建议使用Reader 和 Writer 的单独流。但这不能应用,因为两者应使用相同的管道实例。
那么在这种情况下如何正确管理流呢?
First I write to a NamedPipeClientStream, then I read from it. This basically works. But I don't get the ressource handling of the StreamReader and StreamWriter right.
Approach 1
using (StreamWriter sw = new StreamWriter(pipeStream))
{
// ...
using (StreamReader sr = new StreamReader(pipeStream))
{
// ...
}
}
The disposal of sw
fails, because the stream has already been closed.
Approach 2
using (StreamWriter sw = new StreamWriter(pipeStream))
{
// ...
}
using (StreamReader sr = new StreamReader(pipeStream))
{
// ...
}
Now the pipe server in another process struggles, because the pipe connection was closed prematurely.
Approach 3
In another question regarding file streams it was suggested to use a separate stream for Reader and Writer. But this cannot be applied, since both shall use the same pipe instance.
So how are the streams managed correctly in this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用可以处理单个流的读取和写入的
FileStream
?Why not use a
FileStream
that can handle both reading and writing to a single stream?