tags:

views:

167

answers:

2

Simple question: I want to open a URL using the Default Browser, so I just do Process.Start(url). However, I noticed that this returns an IDisposable object.

So now I wonder if I have to dispose it? Or, for that matter, if my Application is in any way responsible for this process? The intended functionality is simply "Fire and forget", I do not want to have my application as a parent of the new process and it does not need to interact with it.

I've seen some similar but unrelated questions on SO that seem to say that simply calling Process.Start on a URL is fine, but I do not want to run into some hard to debug memory leaks/resource exhaustion issues caused my me program keeping references to long dead browser processes.

+3  A: 

Couldn't you just wrap it in a using clause to ensure the GC does whatever it needs to do with it IF you are required to dispose of it? This would still allow a sort of "fire and forget" but not leave memory/resources in a bad state.

Probably overkill but there is a really good article on CodeProject about the IDisposable interface: http://www.codeproject.com/KB/dotnet/idisposable.aspx

Fooberichu
The problem is that I do not fully understand the object life in this situation. If I do using(Process.Start(url)); then will it wait at that point? Or will it dispose the process to early? At the moment, that makes no difference in behavior so I _guess_ that there are no resources that are kept open, but I don't know for sure and i do not know how to measure that.
Michael Stum
Since the "using" clause implicitly instructs the compiler to build a try/finally and implement a dispose and the fact that they are returning an IDisposable object I think you'd be safe to do that. By not deallocating it you may be tying resources up. You could try building a loop that opens several URLs without disposing them and see if your resources balloon out of control and another test by wrapping them in using clauses. The caveat is you'll have a bunch of windows to close after that. :)
Fooberichu
+2  A: 

Starting the process is a native call which returns a native process handle, which is stored in the instance of Process that is returned. There are methods in Process that use the handle so you can do things like wait for the process to exit, or become idle.

Disposing the Process frees that handle. I agree with Jon, wrap it in a using clause.

Matt