views:

70

answers:

2

Hello

I am interested if calling Process.Refresh() is mandatory when waiting for the process to terminate by checking Process.HasFinished property?

I have a piece of code that works fine without the Process.Refresh() call, however I am curious weather this is a coincidence? I can see that a msdn example has the Process.Refresh() call...

If its not necessary, and Process.HasExited is the only property I need, are there any advantages to making the call to Process.Refresh() ? If not, is there a reason it is in the msdn example?

Thank you for your answers.

+2  A: 

Hi,

as you've noticed, MSDN doesn't exactly specify, what information is cached and thus needs to be refreshed using Process.Refresh().

After a short (and probably incomplete) analysis of the class using reflector it seems though, that the HasExited property is "calculated" every time it's accessed. Refresh seems mostly to force an update of the internal ProcessInfo object, which contains information such as memory consumption, handle information etc.

On the other hand, Refresh doesn't really fetch all this information, it simply discards the internal cache. The information is only refetched when you access any of the properties. So Refresh has practically no performance overhead. Therefore it might be more secure to call Refresh just in case Microsoft decides to change the HasExited implementation in the future.

MartinStettner
A: 

Does not directly answer your question, but if you want to wait until a process terminated you can use the WaitForExit() method of the Process class.

Stefan Egli
I know that, however since the called process runs for a long period of time, I want to use HasExited in order to keep my application from becoming unresponsive.
Rekreativc