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?