如果我没有在我的C#控制台应用程序关闭的System.Diagnostics.Process会发生什么? [英] What happens if I don't close a System.Diagnostics.Process in my C# console app?

查看:74
本文介绍了如果我没有在我的C#控制台应用程序关闭的System.Diagnostics.Process会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统使用的System.Diagnostics.Process运行另一个exe文件一个C#应用程序。我跑进一些例如code,其中的过程是在一个try块启动和关闭在finally块。我也看到了例如code,其中的过程中没有关闭。

I have a C# app which uses a System.Diagnostics.Process to run another exe. I ran into some example code where the process is started in a try block and closed in a finally block. I also saw example code where the process is not closed.

在过程中没有关闭,会发生什么?

What happens when the process is not closed?

是由使用过程中的资源时创建的过程中,控制台应用程序被关闭回收?

Are the resources used by the process reclaimed when the console app that created the process is closed?

它是坏的,打开很多程序,并在一个控制台应用程序这是开放的很长一段时间没有关闭任何人?

Is it bad to open lots of processes and not close any of them in a console app that's open for long periods of time?

干杯!

推荐答案

在其他进程中的退出的,所有的及其的资源被释放,但你仍然会持有到一个进程句柄(这是一个指向有关过程的信息块),除非你调用关闭()过程引用。我怀疑会有大问题,但你不妨 过程工具的IDisposable 所以你可以使用C#的使用(...)语句,它会自动调用的Dispose (因此关闭())为您提供:

When the other process exits, all of its resources are freed up, but you will still be holding onto a process handle (which is a pointer to a block of information about the process) unless you call Close() on your Process reference. I doubt there would be much of an issue, but you may as well. Process implements IDisposable so you can use C#'s using(...) statement, which will automatically call Dispose (and therefore Close()) for you :

using (Process p = Process.Start(...))
{
  ...
}

作为一个经验法则:如果有什么工具的IDisposable ,你真的的通话的Dispose / 关闭或使用使用(...)就可以了。

As a rule of thumb: if something implements IDisposable, You really should call Dispose/Close or use using(...) on it.

这篇关于如果我没有在我的C#控制台应用程序关闭的System.Diagnostics.Process会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆