Is the Close()
method required for the System.Diagnostics.Process class?
EDIT: The Process
is monitoring a console application which terminates almost instantly...
Billy3
Is the Close()
method required for the System.Diagnostics.Process class?
EDIT: The Process
is monitoring a console application which terminates almost instantly...
Billy3
It's not fatal - the garbage collector will call Process
's finalizer eventually, and the object's resources will be cleaned up that way.
It's good practice, though, to free up the resources as soon as they're not needed, by calling Close
directly, or wrapping the code in a using
block. This way you reduce the load on the garbage collector, and you free up a little bit of memory earlier rather than later.
No, not necessarily.
It is typically a good practice to always call Dispose() (or wrap in a using statement) any IDisposable, such as a Process instance. However, if this is properly written, the GC will eventually perform all of the cleanup in the finalizer, even if you do not.
Depending on the resources allocated, this may be a significant "hit" on memory/resources for a period of time, or not. In the case of a Process, you're going to be holding onto a process handle (which is relatively small) until the GC collects and finalizes the object, which probably will not have much of an impact on your program overall.
Close
(or Dispose
) will release (granted probably a relatively small amount of) operating system wide resources. These are things that you share with every other process on the system, so to be a good citizen you should release them in a timely as possible fashion.
Ultimately though they will be released by way of the garbage collector running finalizers or when your process terminates.
Yes.
Note, however, that Close
simply has to do with releasing resources that are local to your application. It does not attempt to terminate the process. To do that, you'd have to call either CloseMainWindow()
(for GUI applications) or Kill()
(for any application).
The Process
class implements IDisposable
, which, strictly speaking, obligates you to calling Dispose
once you're finished with the object. However, for many classes like this, the public-facing version of the Dispose
method is Close
.
The easiest (and most reliable) way of dealing with such objects is with a using
block. This will automatically call Dispose
on the variable as soon as the end of the block is reached. It also protects you from an exception preventing you from disposing of the object.
For example:
using(System.Diagnostics.Process process = new System.Diagnostics.Process())
{
// set your properties and launch it
// wait for it to exit if you want to, or just let it continue to run
}
This is functionally equivalent to doing this:
System.Diagnostics.Process process = new System.Diagnostics.Process();
try
{
// set your properties and launch it
// wait for it to exit if you want to, or just let it continue to run
}
finally
{
process.Close();
}
(Note that I said functionally equivalent, not semantically equivalent; in reality, the using
block maintains its own handle to the object as an IDisposable
, then calls Dispose
, not Close
. That, however, is not relevant to this question.)