简体   繁体   English

Process.Dispose()实际上做了什么?

[英]What does Process.Dispose() actually do?

In C# class Process inherits from class Component that implements IDisposable and so I can call Dispose() on any Process object. 在C# class Process继承自实现IDisposable class Component ,因此我可以在任何Process对象上调用Dispose() Do I really have to? 我真的需要吗? How do I know if I really have to? 我怎么知道我是否真的需要?

Suppose I have the following code: 假设我有以下代码:

 var allProcesses = System.Diagnostics.Process.GetProcesses();
 var processesNames = processes.Select( p => p.ProcessName );
 // output process names here

Now it looks like I have an array of Process objects and I have craft a try-finally to traverse the array and Dispose() each object. 现在看起来我有一个Process对象数组,我已经制作了一个try-finally来遍历数组和Dispose()每个对象。 That's definitely lots of extra code. 这肯定是很多额外的代码。

What does that Dispose() do for Process objects? Dispose()Process对象做了什么? Do I really need to Dispose() every Process object and how do I decide if I need to do so? 我是否真的需要Dispose()每个Process对象,我如何决定是否需要这样做?

Do I really need to Dispose() every Process object and how do I decide if I need to do so? 我是否真的需要Dispose()每个Process对象,我如何决定是否需要这样做?

Yes, you should dispose them. 是的,你应该处理它们。 Note this text in the documentation for Process : 请注意Process的文档中的此文本:

A system process is uniquely identified on the system by its process identifier. 系统进程通过其进程标识符在系统上唯一标识。 Like many Windows resources, a process is also identified by its handle, which might not be unique on the computer. 与许多Windows资源一样,进程也由其句柄标识,该句柄在计算机上可能不是唯一的。 A handle is the generic term for an identifier of a resource. 句柄是资源标识符的通用术语。 The operating system persists the process handle, which is accessed through the Handle property of the Process component, even when the process has exited. 操作系统持久保存进程句柄,即使进程已退出,也可通过Process组件的Handle属性访问该句柄。 Thus, you can get the process's administrative information, such as the ExitCode (usually either zero for success or a nonzero error code) and the ExitTime. 因此,您可以获取进程的管理信息,例如ExitCode(通常为成功为零或非零错误代码)和ExitTime。 Handles are an extremely valuable resource, so leaking handles is more virulent than leaking memory. 手柄是非常有价值的资源,因此泄漏的手柄比泄漏记忆更具毒性。

So if you don't Dispose them, you're potentially leaking the handles (until they're garbage collected - but the whole point of Dispose is to allow early cleanup of resources) 因此,如果你不Dispose它们,你可能会泄漏句柄(直到它们被垃圾收集 - 但Dispose的全部意义是允许尽早清理资源)


Note, also, that the same documentation indicates that Process overrides Dispose(bool) - another clue that it actually does something when Dispose is called. 另请注意,相同的文档表明Process重写Dispose(bool) - 另一个线索,它实际上在调用Dispose时执行某些操作

from MSDN: http://msdn.microsoft.com/en-us/library/3cc9y48w(v=vs.80).aspx 来自MSDN: http//msdn.microsoft.com/en-us/library/3cc9y48w (v= vs.80).aspx

Call Dispose when you are finished using the Component. 完成使用组件后调用Dispose。 The Dispose method leaves the Component in an unusable state. Dispose方法使Component处于不可用状态。 After calling Dispose, you must release all references to the Component so the garbage collector can reclaim the memory that the Component was occupying. 在调用Dispose之后,必须释放对Component的所有引用,以便垃圾收集器可以回收Component占用的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM