当执行的Process.Start(),你需要将它包装一个使用? [英] When doing a Process.Start() do you need to wrap it in a using?

查看:94
本文介绍了当执行的Process.Start(),你需要将它包装一个使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你开始一个过程,不要计较结果是这样行吗?

 的Process.Start(XXX);
 

还是应该做到这一点。

 使用(的Process.Start(XXX)){}
 

解决方案

纵观 Process.dispose(布尔)方法的实现表明,它调用关闭()进程实例。这又清理本机进程的​​句柄所以它可能不是一个坏主意。

这也清理,使用它来检查进程已退出等待处理。

即使你不使用使用(...)阻止终结将在年底赶上这些资源。

When you are starting a process and dont care about the result is this ok?

Process.Start(xxx);

Or should you do this

using (Process.Start(xxx)){}

解决方案

Looking at the implementation of the Process.dispose(bool) method shows that it calls Close() on the Process instance. This in turn cleans up the native process handle so its probably not a bad idea.

It also cleans up a wait handle that it uses to check if the process has exited.

Even if you don't use the using (...) block the finalizer will catch these resources in the end.

这篇关于当执行的Process.Start(),你需要将它包装一个使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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