Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When doing a Process.Start() do you need to wrap it in a using?

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)){}
like image 956
Simon Avatar asked Nov 04 '09 05:11

Simon


2 Answers

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.

like image 77
Luke Quinane Avatar answered Nov 03 '22 00:11

Luke Quinane


The Process object that is returned by Process.Start contains a Windows process HANDLE, so it should be disposed once you no longer need to use the Process object.

If you don't need to use the returned Process object at all, then the empty using block that you show is fine. Note that disposing the Process releases the handle but it (fortunately) does not stop the process from executing.

like image 43
Jason Kresowaty Avatar answered Nov 02 '22 23:11

Jason Kresowaty