what is the differcence between close and dispose with Process instance?
-
here is the code: Process p = Process.GetProcessByID(); ... p.close(); p.dispose(); i use close() and dispose() both to release resource.is it correct ? what is the difference between them ? and in this code: Process[] p = Process.GetProcessesByName(); How to release the resource properly ?
-
here is the code: Process p = Process.GetProcessByID(); ... p.close(); p.dispose(); i use close() and dispose() both to release resource.is it correct ? what is the difference between them ? and in this code: Process[] p = Process.GetProcessesByName(); How to release the resource properly ?
Looking at the IL (Intermediate Language) for the
Process
class reveals that callingDispose
explicitly callsClose
. If you don't call either and the destructor callsDispose
, then the process handle is freed but other native resources are not ( memory leak! :eek: ). CallingClose
is sufficient, but it never hurts to callDispose
either. :)Microsoft MVP, Visual C# My Articles
-
here is the code: Process p = Process.GetProcessByID(); ... p.close(); p.dispose(); i use close() and dispose() both to release resource.is it correct ? what is the difference between them ? and in this code: Process[] p = Process.GetProcessesByName(); How to release the resource properly ?
Here's a tip, use a
using
block to make it both easier and safer to release Disposable resources:using (Process p = Process.GetProcessByID())
{
...
}No need to worry about calling Dispose (or Close). :) Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
-
Looking at the IL (Intermediate Language) for the
Process
class reveals that callingDispose
explicitly callsClose
. If you don't call either and the destructor callsDispose
, then the process handle is freed but other native resources are not ( memory leak! :eek: ). CallingClose
is sufficient, but it never hurts to callDispose
either. :)Microsoft MVP, Visual C# My Articles
-
Thanks! Looking at the IL (Intermediate Language) for the Process class reveals that calling Dispose explicitly calls Close where can i find the IL help ?
It's not "help" - it's the intermediate language contained in the modules that are embedded in assemblies - the very heart of the Common Language Infrastructure (CLI) that makes .NET possible. You can use the IL Disassembler (ildasm.exe) that ships with the .NET Framework SDK, in the SDK's bin directory. Documentation about the IL instruction codes can be found in the .NET Framework SDK.
Microsoft MVP, Visual C# My Articles