Is the Close() method required for the System.Diagnostics.Process class?
EDIT: The Process is monitoring a console application which terminates almost instantly…
Billy3
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes.
Note, however, that
Closesimply 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 eitherCloseMainWindow()(for GUI applications) orKill()(for any application).The
Processclass implementsIDisposable, which, strictly speaking, obligates you to callingDisposeonce you’re finished with the object. However, for many classes like this, the public-facing version of theDisposemethod isClose.The easiest (and most reliable) way of dealing with such objects is with a
usingblock. This will automatically callDisposeon 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:
This is functionally equivalent to doing this:
(Note that I said functionally equivalent, not semantically equivalent; in reality, the
usingblock maintains its own handle to the object as anIDisposable, then callsDispose, notClose. That, however, is not relevant to this question.)