I've seen a lot of articles and questions about how to be sure that Excel actually quits when you want it to and the process doesn't stay alive. Here is a knowledge Base article describing the problem and Microsoft's recommended solution. Essentially:
'close files
'Quit Excel
xlApp.quit()
'Release and collect garbage
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
Many people don't recommend killing the process; See How to properly clean up Excel interop objects and Understanding Garbage Collection in .net
On the other hand many people don't recommend using GC.Collect. See What's so wrong about using GC.Collect()?
In my experience killing the process is the fastest and easiest way to be sure that Excel is gone. My code kills only the exact process that it starts, no other. I make sure to Close any open workbooks, Quit the application and Release the xlApp object. Finally I check to see if the process is still alive and if so then kill it.
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, _
ByRef lpdwProcessId As Integer) As Integer
End Function
Sub testKill()
'start the application
Dim xlApp As Object = CreateObject("Excel.Application")
'do some work with Excel
'close any open files
'get the window handle
Dim xlHWND As Integer = xlApp.hwnd
'this will have the process ID after call to GetWindowThreadProcessId
Dim ProcIdXL As Integer = 0
'get the process ID
GetWindowThreadProcessId(xlHWND, ProcIdXL)
'get the process
Dim xproc As Process = Process.GetProcessById(ProcIdXL)
'Quit Excel
xlApp.quit()
'Release
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp)
'set to nothing
xlApp = Nothing
'kill the process if still running
If Not xproc.HasExited Then
xproc.Kill()
End If
End Sub
I've seen a lot of people say that killing the process is bad, but I haven't seen any qualitative answers on why. Especially after making sure that files are closed, Excel has quit and we'll only kill the exact process that we started. My question is what are the potential problems with killing the Excel process. Does it hurt perfomance? Will it harm Excel?
Many will also say that with good coding I don't have to kill the process. Maybe, but that doesn't answer the question of "Why is it bad to kill the process?" After closing the files, quitting Excel and releasing the objects; why would it be a bad thing to just make absolutely sure the process is gone?
Edit: Also what is actually left after Excel quits? If Excel was visible, it appears to quit normally, disappearing from view and from the taskbar. So did Excel actually quit or didn't it. It seems to me that Excel actually did quit and that we only have an empty process shell running. Can anyone comment on that?
Edit: It is interesting to me to note that GC (aka Garbage Collection) via GC.Collect() GC.WaitForPendingFinalizers() will actually release the process shell that is left behind after Excel quits. Does that support my assumption that the empty process shell really is garbage after all?
Edit: just found an excellent website on the problem: 50 Ways to kill Excel