如果我杀死.Kill()一个的System.Diagnostics.Process,我还需要调用.Close()? [英] If I kill a System.Diagnostics.Process with .Kill(), do I also need to call .Close()?

查看:1127
本文介绍了如果我杀死.Kill()一个的System.Diagnostics.Process,我还需要调用.Close()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#4.0中,我创建了一个的System.Diagnostics.Process ,我希望把时间的运行很短的金额。如果由于某种原因,过程时间(例如一定量后,一直没有退出,我叫 .WaitForExit(超时)键,返回值是),我需要清理。我已经决定,它的安全使用 .Kill()在这种特殊情况下(没有数据结构,我很担心腐败)。

Using C# 4.0, I've created a System.Diagnostics.Process that I expect to take a short amount of time to run. If for some reason the process hasn't exited after some amount of time (e.g, I've called .WaitForExit(timeout) and the return value was false), I need to cleanup. I've decided that it's safe to use .Kill() in this particular situation (there are no data structures I'm worried about corrupting).

鉴于此设置,我还需要调用进程的 .Close()的方法?如果是这样,我应该叫 .Close()之前或之后 .Kill()

Given this setup, do I also need to call the .Close() method of the Process? If so, should I call .Close() before or after .Kill()?

推荐答案

的System.Diagnostics.Process 工具的IDisposable 通过组件类,并受保护的的Dispose(布尔)方法调用关闭()。它通常被认为是很好的做法,处置支配的资源,当你与他们做的,因为这将立即释放任何与类(不必等候GC运行)相关联的资源。

System.Diagnostics.Process implements IDisposable via the Component class, and the protected Dispose(bool) method calls Close(). It is generally considered good practice to dispose of disposable resources when you are done with them, as this will immediately release any resources associated with the class (not having to wait for the GC to run).

因此​​,要回答你的问题:

So to answer your question:

是的,请拨打关闭()通过直接或通过C#<调用的Dispose()方法code>使用结构如下:

Yes, please call Close() by calling the Dispose() method directly or via the C# using construct as follows.

using(Process proc = CreateProcess())
{
    StartProcess(proc);
    if (!proc.WaitForExit(timeout))
    {
        proc.Kill();
    }
}

这篇关于如果我杀死.Kill()一个的System.Diagnostics.Process,我还需要调用.Close()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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