不调用 Process.Close() 是否存在重大问题?

发布于 2024-09-07 17:43:01 字数 145 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

混吃等死 2024-09-14 17:43:01

但请注意,Close 仅与释放应用程序本地资源有关。它不会尝试终止该进程。为此,您必须调用 CloseMainWindow()(对于 GUI 应用程序)或 Kill()(对于任何应用程序)。

Process 类实现了 IDisposable,严格来说,它要求您在完成对象后调用 Dispose。但是,对于许多这样的类,Dispose 方法的面向公众的版本是 Close

处理此类对象的最简单(也是最可靠)的方法是使用 using 块。一旦到达块的末尾,就会自动对变量调用Dispose。它还可以保护您免受异常情况的影响,从而阻止您处置该对象。

例如:

using(System.Diagnostics.Process process = new System.Diagnostics.Process())
{
    // set your properties and launch it
    // wait for it to exit if you want to, or just let it continue to run
}

这在功能上等同于这样做:(

System.Diagnostics.Process process = new System.Diagnostics.Process();

try
{
    // set your properties and launch it
    // wait for it to exit if you want to, or just let it continue to run
}
finally
{
    process.Close();
}

请注意,我说的是功能上等同,而不是语义上等同;实际上,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 either CloseMainWindow() (for GUI applications) or Kill() (for any application).

The Process class implements IDisposable, which, strictly speaking, obligates you to calling Dispose once you're finished with the object. However, for many classes like this, the public-facing version of the Dispose method is Close.

The easiest (and most reliable) way of dealing with such objects is with a using block. This will automatically call Dispose 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:

using(System.Diagnostics.Process process = new System.Diagnostics.Process())
{
    // set your properties and launch it
    // wait for it to exit if you want to, or just let it continue to run
}

This is functionally equivalent to doing this:

System.Diagnostics.Process process = new System.Diagnostics.Process();

try
{
    // set your properties and launch it
    // wait for it to exit if you want to, or just let it continue to run
}
finally
{
    process.Close();
}

(Note that I said functionally equivalent, not semantically equivalent; in reality, the using block maintains its own handle to the object as an IDisposable, then calls Dispose, not Close. That, however, is not relevant to this question.)

苏佲洛 2024-09-14 17:43:01

不,不一定。

通常,最好始终调用任何 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.

生生漫 2024-09-14 17:43:01

这不是致命的 - 垃圾收集器最终将调用 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 a using block. This way you reduce the load on the garbage collector, and you free up a little bit of memory earlier rather than later.

谷夏 2024-09-14 17:43:01

Close(或Dispose)将释放(可能是相对少量的)操作系统范围的资源。这些是您与系统上的所有其他进程共享的东西,因此作为一个好公民,您应该尽可能及时地发布它们。

最终,它们将通过运行终结器的垃圾收集器或当您的进程终止时被释放。

Close (or Dispose) 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文