以下代码正在生成警告。问题是我们需要管道读取和写入。我怎样才能安全地处理管道?丢弃具有2个流的对象
警告:CA2202:Microsoft.Usage:对象'pipe'可以在方法'ClientConnection.qaz()'中多次处置。为了避免产生System.ObjectDisposedException你不应该调用Dispose不止一次对象:线路上:465
void qaz()
{
const string THIS_SERVER = ".";
using (NamedPipeClientStream pipe = new NamedPipeClientStream(THIS_SERVER, this.Name,
PipeDirection.InOut,
PipeOptions.None))
{
using (StreamReader sr = new StreamReader(pipe))
{
string message = sr.ReadLine();
using (StreamWriter sw = new StreamWriter(pipe))
{
sw.WriteLine("ACK received");
}
}
}
}
您需要的Visual Studio代码分析看到这些警告(这些都不是C#编译器警告)。
的问题是,所述的StreamReader SR和的StreamWriter SW对象管的两端丢弃。
是的,忽略CA2202。这是一个愚蠢的警告。 – porges 2012-02-07 11:03:59
sr和sw都会处理对象管道,所以仍然存在问题。 – David 2012-02-07 11:05:38