in company common way release excel interop objects use idisposable following way:
public sub dispose() implements idisposable.dispose if not boldisposed finalize() system.gc.suppressfinalize(me) end if end sub protected overrides sub finalize() _xlapp = nothing boldisposed = true mybase.finalize() end sub where _xlapp created in constructor following way:
try _xlapp = ctype(getobject(, "excel.application"), excel.application) catch e exception _xlapp = ctype(createobject("excel.application"), excel.application) end try and client uses using-statement execute code concerning excel interop objects.
we avoid use two dot rule. started researching how realease (excel) interop objects , discussions found how clean excel interop objects or release excel objects using marshal.releasecomobject(), none of them using idisposable interface.
my questions is: there disadvantages using idisposable interace releasing excel interop objects? if so, these disatvantages.
are there disadvantages using idisposable interace
sure, accomplishes absolutely nothing. using using or calling dispose() never appropriate way set variable nothing. code does.
we avoid use 2 dot rule.
feel free continue ignore it, nonsense , causes nothing grief. blog author's implied assertion doing force programmer use variable store value of xlapp.workbooks. he'd have fighting chance, later, not forget call releaseobject(). there many more statements produce interface reference don't use dots. range(x,y), there's hidden range object reference there you'll never see. having store them produces incredibly convoluted code.
and overlooking one enough fail job done. utterly impossible debug. kind of code c programmers have write. , failed @ miserably, large c programs leak memory , programmers spend great deal of time finding leaks. not .net way of course, has garbage collector automatically. never gets wrong.
trouble is, bit slow @ taking care of job. design. nobody ever notices this, except in kind of code. can see garbage collector didn't run, still see office program running. didn't quit when wrote xlapp.quit(), still present in processes tab of task manager. want happen quit when so.
that's possible in .net, can force gc job done:
gc.collect() gc.waitforpendingfinalizers() boom, every excel object reference gets released automatically. there no need store these object references , explicitly call marshal.releasecomobject(), clr you. , never gets wrong, doesn't use or need "two dot rule" , has no trouble finding hidden interface references back.
what matters great deal exactly put code. , programmers put in wrong place, in same method used excel interfaces. fine, not work when debug code, quirk that's explained in this answer. proper way in blog author's code move code little helper method, let's call doexcelthing(). this:
private sub button1_click(byval sender system.object, byval e system.eventargs) handles button1.click doexcelthing() gc.collect() gc.waitforpendingfinalizers() '' excel.exe no longer running anymore @ point end sub and keep in mind debugging artifact. programmers hate have use task manager kill zombie excel.exe instances. zombified when stopped debugger, preventing program exiting , collect garbage. normal. happen when program dies in production kind of reason. put energy belongs, getting bugs out of code program won't die. gc doesn't need more that.
Comments
Post a Comment