通过 using 块使用 Process

发布于 2024-11-04 13:11:06 字数 1138 浏览 4 评论 0原文

可能的重复:
会发生什么如果我不关闭 C# 控制台应用程序中的 System.Diagnostics.Process?

因为 System.Diagnostics.Process 继承自实现 Component IDisposable,我是否应该始终使用 using 块创建一个 Process

例如,这个...:

using (var process = new Process())
{
    process.StartInfo.FileName = "some process.exe";
    process.Start();
    process.WaitForExit();
}

...而不是这个:

var process = new Process
    { 
        StartInfo = { FileName = "some process.exe" }
    };
process.Start();
process.WaitForExit();

我问是因为我很少看到 Process 出现在 using 块中;例如 MSDN 页面 Process 不使用它。使用对象初始值设定项也很有帮助。

如果我应该使用它,我是否应该将其“改造”到我现有的代码库中?

如果不这样做,可能会产生什么后果? (假设在每种情况下都正确调用了 WaitForExit()。)

Possible Duplicate:
What happens if I don't close a System.Diagnostics.Process in my C# console app?

As System.Diagnostics.Process inherits from Component which implements IDisposable, should I always create a Process with a using block?

For example, this...:

using (var process = new Process())
{
    process.StartInfo.FileName = "some process.exe";
    process.Start();
    process.WaitForExit();
}

...instead of this:

var process = new Process
    { 
        StartInfo = { FileName = "some process.exe" }
    };
process.Start();
process.WaitForExit();

I ask because I've rarely seen Process appear in a using block; for example the MSDN page for Process does not use it. Having the use of the object initializer is helpful too.

If I should be using it, should I then go and 'retrofit' it to my existing codebase?

What might be the consequences if this were not done? (Assuming WaitForExit() is being called correctly in each case.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

三生池水覆流年 2024-11-11 13:11:06

如果您不使用或不能使用 using(),则应确保在不再需要流程变量时将其释放。

如果在类(而不是程序或方法)中使用流程变量,则该类应实现 IDisposable,然后在其 Dispose(bool) 方法中调用 _process.Dispose:

void Dispose(bool disposing)
{
    ...
    if (_process != null)
    {
        Dispose(_process);
    }
}

如果没有 _process 字段,而只有流程变量如果您在方法中使用了它,则必须在方法内将其丢弃:

void MyMethod()
{
    var process = ...
    ... use it here ...
    process.Dispose();
}

If you don't or cannot use using(), you should make sure you Dispose the process variable when it is no longer needed.

If you use the process variable in a class (instead of a Program or a method), then that class should implement IDisposable and then call _process.Dispose in its Dispose(bool) method:

void Dispose(bool disposing)
{
    ...
    if (_process != null)
    {
        Dispose(_process);
    }
}

If there is no _process field but only a process variable that you use in your method, you must dispose of it inside the method:

void MyMethod()
{
    var process = ...
    ... use it here ...
    process.Dispose();
}
彼岸花似海 2024-11-11 13:11:06

MSDN 的例子是人为的。打开进程句柄的程序一旦启动进程就退出。当该程序退出时,它打开的所有句柄都将关闭。

如果打开进程句柄,则应该将其关闭。 Component.Dispose 的 Process.Dispose 重写只是调用 Process.Close。 using 语句简化了这一点。

The MSDN example is contrived. The program which opens a process handle is exiting as soon as it starts the process. When that program exits, any handles it opened are closed.

If you open a process handle, you should close it. The Process.Dispose override of Component.Dispose simply calls Process.Close. The using statement simplifies this.

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