c# - Should I use a using statement/dispose if the object isn't actually going to be used in code? -


this question has answer here:

i'm working in winforms code right now, , have come across lines several times:

... system.diagnostics.process.start(somefilename); ... 

...which kicks off process on client machine, , forgets it. process.start(somefilename) returns process type object, implements idisposable, code not doing object, because whole idea trigger process , forget it.

in case, should process object still disposed, if we're not keeping in memory @ all? like:

using (system.diagnostics.process.start(somefilename) { } 

or

var process = system.diagnostics.process.start(somefilename); process.dispose(); 

process objects holds unmanaged resource(s) (hprocess handle)

https://referencesource.microsoft.com/#system/services/monitoring/system/diagnosticts/process.cs,f8b2e604d6f1fe04

public class process : component {     //     // fields     //      bool haveprocessid;     int processid;     bool haveprocesshandle;     safeprocesshandle m_processhandle; // <- should disposed     bool isremotemachine;     string machinename;     processinfo processinfo;     int32 m_processaccess; ... 

and should disposed. using local variable (var process = ...) looks overshooting in context, that's why suggest

system.diagnostics.process.start(somefilename).close(); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -