不调用 Process.Close() 是否存在重大问题?
System.Diagnostics.Process 类是否需要 Close()
方法?
编辑: Process
正在监视一个控制台应用程序,该应用程序几乎立即终止......
Billy3
Is the Close()
method required for the System.Diagnostics.Process class?
EDIT: The Process
is monitoring a console application which terminates almost instantly...
Billy3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是。
但请注意,
Close
仅与释放应用程序本地资源有关。它不会尝试终止该进程。为此,您必须调用CloseMainWindow()
(对于 GUI 应用程序)或Kill()
(对于任何应用程序)。Process
类实现了IDisposable
,严格来说,它要求您在完成对象后调用Dispose
。但是,对于许多这样的类,Dispose
方法的面向公众的版本是Close
。处理此类对象的最简单(也是最可靠)的方法是使用
using
块。一旦到达块的末尾,就会自动对变量调用Dispose
。它还可以保护您免受异常情况的影响,从而阻止您处置该对象。例如:
这在功能上等同于这样做:(
请注意,我说的是功能上等同,而不是语义上等同;实际上,
using
块将其自己的对象句柄维护为IDisposable< /code>,然后调用
Dispose
,而不是Close
。但是,这与这个问题无关。)Yes.
Note, however, that
Close
simply has to do with releasing resources that are local to your application. It does not attempt to terminate the process. To do that, you'd have to call eitherCloseMainWindow()
(for GUI applications) orKill()
(for any application).The
Process
class implementsIDisposable
, which, strictly speaking, obligates you to callingDispose
once you're finished with the object. However, for many classes like this, the public-facing version of theDispose
method isClose
.The easiest (and most reliable) way of dealing with such objects is with a
using
block. This will automatically callDispose
on the variable as soon as the end of the block is reached. It also protects you from an exception preventing you from disposing of the object.For example:
This is functionally equivalent to doing this:
(Note that I said functionally equivalent, not semantically equivalent; in reality, the
using
block maintains its own handle to the object as anIDisposable
, then callsDispose
, notClose
. That, however, is not relevant to this question.)不,不一定。
通常,最好始终调用任何 IDisposable(例如 Process 实例)的 Dispose() (或包装在 using 语句中)。但是,如果编写正确,GC 最终将在终结器中执行所有清理,即使您不这样做。
根据分配的资源,这可能会在一段时间内对内存/资源造成重大“影响”,也可能不会。对于进程,您将保留进程句柄(相对较小),直到 GC 收集并最终确定对象,这可能不会对您的程序整体产生太大影响。
No, not necessarily.
It is typically a good practice to always call Dispose() (or wrap in a using statement) any IDisposable, such as a Process instance. However, if this is properly written, the GC will eventually perform all of the cleanup in the finalizer, even if you do not.
Depending on the resources allocated, this may be a significant "hit" on memory/resources for a period of time, or not. In the case of a Process, you're going to be holding onto a process handle (which is relatively small) until the GC collects and finalizes the object, which probably will not have much of an impact on your program overall.
这不是致命的 - 垃圾收集器最终将调用 Process 的终结器,并且对象的资源将以这种方式被清理。
不过,最好在不需要资源时立即释放资源,方法是直接调用
Close
或将代码包装在using
块中。通过这种方式,您可以减少垃圾收集器的负载,并且可以更早而不是更晚地释放一点内存。It's not fatal - the garbage collector will call
Process
's finalizer eventually, and the object's resources will be cleaned up that way.It's good practice, though, to free up the resources as soon as they're not needed, by calling
Close
directly, or wrapping the code in ausing
block. This way you reduce the load on the garbage collector, and you free up a little bit of memory earlier rather than later.Close
(或Dispose
)将释放(可能是相对少量的)操作系统范围的资源。这些是您与系统上的所有其他进程共享的东西,因此作为一个好公民,您应该尽可能及时地发布它们。最终,它们将通过运行终结器的垃圾收集器或当您的进程终止时被释放。
Close
(orDispose
) will release (granted probably a relatively small amount of) operating system wide resources. These are things that you share with every other process on the system, so to be a good citizen you should release them in a timely as possible fashion.Ultimately though they will be released by way of the garbage collector running finalizers or when your process terminates.