C# Process.Dispose() 是否也清理 StandardInput 和 StandardInput.BaseStream?
C# Does Process.Dispose() also cleanup the StandardInput and StandardInput.BaseStream?
目前正在处理我正在启动的进程,然后访问 StandardInput.BaseStream
,然后将 Stream
复制到它。我是否需要 StandardInput
的 Dispose
and/or 和 StandardInput.BaseStream
的 Dispose
还是用 Process.Dispose()
处理的?
Process someProgram = null;
try
{
someProgram = new Process();
someProgram.StartInfo.RedirectStandardInput = true;
someProgram.StartInfo.FileName = @"C:\Temp\SomeProgram.exe";
someProgram.Start();
streamParamater.CopyTo(someProgram.StandardInput.BaseStream);
someProgram.WaitForExit();
}
catch
{
// Error Logging
}
finally
{
if (someProgram != null)
{
someProgram.Dispose();
}
streamParamater.Dispose();
}
读取器/写入器及其基本流不会通过在 Process
实例上调用 Close()
或 Dispose()
来处理。
Process.Close()
方法只是将引用设置为 null,这样一旦没有其他引用,GC 就可以收集它们。
source code of Process.Close()
里也有这个评论:
//Don't call close on the Readers and writers
//since they might be referenced by somebody else while the
//process is still alive but this method called.
因此,如果您想确保尽快释放资源,则必须对读者/作者调用 Dispose()
。
目前正在处理我正在启动的进程,然后访问 StandardInput.BaseStream
,然后将 Stream
复制到它。我是否需要 StandardInput
的 Dispose
and/or 和 StandardInput.BaseStream
的 Dispose
还是用 Process.Dispose()
处理的?
Process someProgram = null;
try
{
someProgram = new Process();
someProgram.StartInfo.RedirectStandardInput = true;
someProgram.StartInfo.FileName = @"C:\Temp\SomeProgram.exe";
someProgram.Start();
streamParamater.CopyTo(someProgram.StandardInput.BaseStream);
someProgram.WaitForExit();
}
catch
{
// Error Logging
}
finally
{
if (someProgram != null)
{
someProgram.Dispose();
}
streamParamater.Dispose();
}
读取器/写入器及其基本流不会通过在 Process
实例上调用 Close()
或 Dispose()
来处理。
Process.Close()
方法只是将引用设置为 null,这样一旦没有其他引用,GC 就可以收集它们。
source code of Process.Close()
里也有这个评论:
//Don't call close on the Readers and writers
//since they might be referenced by somebody else while the
//process is still alive but this method called.
因此,如果您想确保尽快释放资源,则必须对读者/作者调用 Dispose()
。