c# - Why is killing the Excel process a bad thing? -


i've seen lot of articles , questions how sure excel quits when want , process doesn't stay alive. here knowledge base article describing problem , microsoft's recommended solution. essentially:

'close files  'quit excel xlapp.quit()  'release , collect garbage system.runtime.interopservices.marshal.finalreleasecomobject(xlapp) gc.collect() gc.waitforpendingfinalizers() 

many people don't recommend killing process; see how clean excel interop objects , understanding garbage collection in .net

on other hand many people don't recommend using gc.collect. see what's wrong using gc.collect()?

in experience killing process fastest , easiest way sure excel gone. code kills exact process starts, no other. make sure close open workbooks, quit application , release xlapp object. check see if process still alive , if kill it.

<system.runtime.interopservices.dllimport("user32.dll", setlasterror:=true)> _ private shared function getwindowthreadprocessid(byval hwnd intptr, _ byref lpdwprocessid integer) integer     end function  sub testkill()      'start application     dim xlapp object = createobject("excel.application")      'do work excel      'close open files      'get window handle     dim xlhwnd integer = xlapp.hwnd      'this have process id after call getwindowthreadprocessid     dim procidxl integer = 0      'get process id     getwindowthreadprocessid(xlhwnd, procidxl)      'get process     dim xproc process = process.getprocessbyid(procidxl)      'quit excel     xlapp.quit()      'release     system.runtime.interopservices.marshal.finalreleasecomobject(xlapp)      'set nothing     xlapp = nothing      'kill process if still running     if not xproc.hasexited         xproc.kill()     end if  end sub 

i've seen lot of people killing process bad, haven't seen qualitative answers on why. after making sure files closed, excel has quit , we'll kill exact process started. question potential problems killing excel process. hurt perfomance? harm excel?

many coding don't have kill process. maybe, doesn't answer question of "why bad kill process?" after closing files, quitting excel , releasing objects; why bad thing make absolutely sure process gone?

edit: left after excel quits? if excel visible, appears quit normally, disappearing view , taskbar. did excel quit or didn't it. seems me excel did quit , have empty process shell running. can comment on that?

edit: interesting me note gc (aka garbage collection) via gc.collect() gc.waitforpendingfinalizers() release process shell left behind after excel quits. support assumption empty process shell garbage after all?

edit: found excellent website on problem: 50 ways kill excel

look, fact of matter is, should let app exit if @ possible. killing app last resort. understand you're convinced don't see wrong doing in case, , maybe you're correct. if there absolutely no negative affects system killing it, doesn't change fact it's wrong thing do. it's breaking law because know won't caught, , it's victimless crime anyways.

there are, however, potential side-effects may not realize. example, when forcefully terminate app, os may retain crash data it. or may send crash telemetry microsoft. you're telling microsoft apps crashing more are, causing crash statistics skewed.

another possible side effect registry hives may not unload correctly. may have seen error in event log time time. typically happens when app forcefully closed , did not close handles registry correctly.

even if none of things happen, can't know future version of os might do. works today, might not work tomorrow. why should always follow documented api's , guidelines, because typically work hard support they've published, typically not work hard support they've told not do.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -