Process.Close() 和 Process.Dispose() 有什么区别?
What are the differences between Process.Close() and Process.Dispose()?
我启动了一个 Process
然后我想确保它能正常关闭。我已经使用 Process.Close()
关闭它,但有时它仍然锁定资源。我可以看到我们有 Process.Dispose()
方法。我只想知道它们之间的 实际差异 是什么,我是否应该同时调用它们以确保关闭该过程?
p.Dispose();
p.Close();
来自 documentation of Process.Close()
;
The Dispose
method calls Close
. Placing the Process
object in a using
block disposes of resources without the need to call Close
.
也就是说,没有区别。据我所知,在内部,.NET 中的所有 Close
方法都调用 Dispose
方法。
如果你看 reference source;
public void Close()
{
...
m_processHandle.Close();
...
}
和这个方法calls;
public void Close() {
Dispose(true);
}
您应该始终对Process
对象使用using
语句。它允许尽早清理资源,因此您无需等到它们被垃圾回收。
通常释放 class 中保存的 UnManaged 资源。它对过程本身没有影响。
到目前为止处理。从 MSDN 中获取的 Close 方法的作用是:
Frees all the resources that are associated with this component.
ref: MSDN Process.Close()
所以从外面看没有区别,但是让我们看看光荣的.net SourceCode:Process
/// <devdoc>
/// <para>
/// Frees any resources associated with this component.
/// </para>
/// </devdoc>
public void Close() {
if (Associated) {
if (haveProcessHandle) {
StopWatchingForExit();
Debug.WriteLineIf(processTracing.TraceVerbose, "Process - CloseHandle(process) in Close()");
m_processHandle.Close();
m_processHandle = null;
haveProcessHandle = false;
}
haveProcessId = false;
isRemoteMachine = false;
machineName = ".";
raisedOnExited = false;
//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.
standardOutput = null;
standardInput = null;
standardError = null;
output = null;
error = null;
Refresh();
}
}
dispose 执行此操作
/// <internalonly/>
/// <devdoc>
/// <para>
/// Free any resources associated with this component.
/// </para>
/// </devdoc>
protected override void Dispose(bool disposing) {
if( !disposed) {
if (disposing) {
//Dispose managed and unmanaged resources
Close();
}
this.disposed = true;
base.Dispose(disposing);
}
}
如您所见,即使在内部也没有区别。 Dispose 只是包装关闭方法。
这是 MSDN 实际上非常有用的案例之一
https://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx
请注意,如果您想使用 "using" 结构:
using (MyResource myRes = new MyResource())
{
myRes.DoSomething();
}
您需要您的对象支持 IDisposable,并且:
The IDisposable interface requires the implementation of a single
parameterless method, Dispose.
Dispose 方法也是垃圾收集器调用的方法,在删除对对象的所有引用之前,确保对象被很好地清理..
因此,IDisposable 的实现及其用法是一种确保在垃圾收集器实际从内存中删除对象之前释放对象所占用的资源的方法。
您可以使用 using
强制执行此操作
genericly close 没有实现这个。
但有时它确实包装了 Dispose 方法:
https://msdn.microsoft.com/en-us/library/system.io.stream.close(v=vs.110).aspx
Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.
对于流程class,情况正好相反。
Difference between Close()
and Dispose()
Method
Close()
Vs Dispose()
Method
Close()
和Dispose()
的基本区别是,当调用Close()
方法时,任何托管资源都可以暂时关闭并可以再次打开。这意味着可以使用相同的对象重新打开或使用资源。而 Dispose()
方法从内存中永久删除任何((非)托管)资源以进行清理,并且该资源不再存在以进行任何进一步处理。
Example showing difference between Close()
and Dispose()
Methods:
using System;
using System.Data;
using System.Data.SqlClient;
public class Test
{
private string connString = "Data Source=COMP3;Initial Catalog=Northwind;User Id=sa;Password=pass";
private SqlConnection connection;
public Test()
{
connection = new SqlConnection(connString);
}
private static void Main()
{
Test t = new Test();
t.ConnectionStatus();
Console.ReadLine();
}
public void ConnectionStatus()
{
try
{
if(connection.State == ConnectionState.Closed)
{
connection.Open();
Console.WriteLine("Connection opened..");
}
if(connection.State == ConnectionState.Open)
{
connection.Close();
Console.WriteLine("Connection closed..");
}
// connection.Dispose();
if(connection.State == ConnectionState.Closed)
{
connection.Open();
Console.WriteLine("Connection again opened..");
}
}
catch(SqlException ex)
{
Console.WriteLine(ex.Message+"\n"+ex.StackTrace);
}
catch(Exception ey)
{
Console.WriteLine(ey.Message+"\n"+ey.StackTrace);
}
finally
{
Console.WriteLine("Connection closed and disposed..");
connection.Dispose();
}
}
}
在上面的例子中,如果你取消注释connection.Dispose()
方法并执行,你会得到一个异常,例如
The ConnectionString
property has not been initialized.
这是Close()
和Dispose()
的区别。
我启动了一个 Process
然后我想确保它能正常关闭。我已经使用 Process.Close()
关闭它,但有时它仍然锁定资源。我可以看到我们有 Process.Dispose()
方法。我只想知道它们之间的 实际差异 是什么,我是否应该同时调用它们以确保关闭该过程?
p.Dispose();
p.Close();
来自 documentation of Process.Close()
;
The
Dispose
method callsClose
. Placing theProcess
object in ausing
block disposes of resources without the need to callClose
.
也就是说,没有区别。据我所知,在内部,.NET 中的所有 Close
方法都调用 Dispose
方法。
如果你看 reference source;
public void Close()
{
...
m_processHandle.Close();
...
}
和这个方法calls;
public void Close() {
Dispose(true);
}
您应该始终对Process
对象使用using
语句。它允许尽早清理资源,因此您无需等到它们被垃圾回收。
通常释放 class 中保存的 UnManaged 资源。它对过程本身没有影响。
到目前为止处理。从 MSDN 中获取的 Close 方法的作用是:
Frees all the resources that are associated with this component. ref: MSDN Process.Close()
所以从外面看没有区别,但是让我们看看光荣的.net SourceCode:Process
/// <devdoc>
/// <para>
/// Frees any resources associated with this component.
/// </para>
/// </devdoc>
public void Close() {
if (Associated) {
if (haveProcessHandle) {
StopWatchingForExit();
Debug.WriteLineIf(processTracing.TraceVerbose, "Process - CloseHandle(process) in Close()");
m_processHandle.Close();
m_processHandle = null;
haveProcessHandle = false;
}
haveProcessId = false;
isRemoteMachine = false;
machineName = ".";
raisedOnExited = false;
//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.
standardOutput = null;
standardInput = null;
standardError = null;
output = null;
error = null;
Refresh();
}
}
dispose 执行此操作
/// <internalonly/>
/// <devdoc>
/// <para>
/// Free any resources associated with this component.
/// </para>
/// </devdoc>
protected override void Dispose(bool disposing) {
if( !disposed) {
if (disposing) {
//Dispose managed and unmanaged resources
Close();
}
this.disposed = true;
base.Dispose(disposing);
}
}
如您所见,即使在内部也没有区别。 Dispose 只是包装关闭方法。
这是 MSDN 实际上非常有用的案例之一
https://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx
请注意,如果您想使用 "using" 结构:
using (MyResource myRes = new MyResource())
{
myRes.DoSomething();
}
您需要您的对象支持 IDisposable,并且:
The IDisposable interface requires the implementation of a single parameterless method, Dispose.
Dispose 方法也是垃圾收集器调用的方法,在删除对对象的所有引用之前,确保对象被很好地清理..
因此,IDisposable 的实现及其用法是一种确保在垃圾收集器实际从内存中删除对象之前释放对象所占用的资源的方法。
您可以使用 using
genericly close 没有实现这个。 但有时它确实包装了 Dispose 方法: https://msdn.microsoft.com/en-us/library/system.io.stream.close(v=vs.110).aspx
Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.
对于流程class,情况正好相反。
Difference between
Close()
andDispose()
Method
Close()
VsDispose()
Method
Close()
和Dispose()
的基本区别是,当调用Close()
方法时,任何托管资源都可以暂时关闭并可以再次打开。这意味着可以使用相同的对象重新打开或使用资源。而 Dispose()
方法从内存中永久删除任何((非)托管)资源以进行清理,并且该资源不再存在以进行任何进一步处理。
Example showing difference between
Close()
andDispose()
Methods:
using System;
using System.Data;
using System.Data.SqlClient;
public class Test
{
private string connString = "Data Source=COMP3;Initial Catalog=Northwind;User Id=sa;Password=pass";
private SqlConnection connection;
public Test()
{
connection = new SqlConnection(connString);
}
private static void Main()
{
Test t = new Test();
t.ConnectionStatus();
Console.ReadLine();
}
public void ConnectionStatus()
{
try
{
if(connection.State == ConnectionState.Closed)
{
connection.Open();
Console.WriteLine("Connection opened..");
}
if(connection.State == ConnectionState.Open)
{
connection.Close();
Console.WriteLine("Connection closed..");
}
// connection.Dispose();
if(connection.State == ConnectionState.Closed)
{
connection.Open();
Console.WriteLine("Connection again opened..");
}
}
catch(SqlException ex)
{
Console.WriteLine(ex.Message+"\n"+ex.StackTrace);
}
catch(Exception ey)
{
Console.WriteLine(ey.Message+"\n"+ey.StackTrace);
}
finally
{
Console.WriteLine("Connection closed and disposed..");
connection.Dispose();
}
}
}
在上面的例子中,如果你取消注释connection.Dispose()
方法并执行,你会得到一个异常,例如
The
ConnectionString
property has not been initialized.
这是Close()
和Dispose()
的区别。