Reputation: 846
To quote an answer to the question "How to open a URL in chrome incognito mode":
I wrote this and it successfull:
Process.Start(@"chrome.exe", "--incognito http://domain.com");
someone replied with this comment:
You need to dispose the object or you'll have a memory leak.
I generally create many processes with Process.Start
but I never "dispose" of them. What is this and how do I do it?
Upvotes: 1
Views: 1912
Reputation: 61349
Per the Component article on MSDN, that is correct. You should dispose of your Process
object after use, releasing any unmanaged resources (the application will not close).
A Component should release resources explicitly by calls to its Dispose method, without waiting for automatic memory management through an implicit call to the Finalize method. When a Container is disposed, all components within the Container are also disposed.
And from the Process
article:
This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.
Process
derives from Component
, so you should dispose it. Its easy enough:
Process myProc = Process.Start(@"chrome.exe", "--incognito http://domain.com");
myProc.Dispose();
or even easier
using (Process myProc = Process.Start(@"chrome.exe", "--incognito http://domain.com"))
{
//Do whatever with the process
}
The using block will dispose the object when the block is exited.
Upvotes: 4
Reputation: 4391
use Process.Close();
var Pro = new Process();
Pro.Start(@"chrome.exe", "--incognito http://domain.com");
Pro.WaitForExit(); //optional
Pro.Close();
Upvotes: 0