I saw a lot of articles and questions on how to be sure that Excel actually quits when you want it, and the process does not stay alive. Here is a knowledge base article describing the problem and a solution recommended by Microsoft. Essentially:
'close files 'Quit Excel xlApp.quit() 'Release and collect garbage System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp) GC.Collect() GC.WaitForPendingFinalizers()
Many people do not recommend killing the process; See How to properly clean Excel interaction objects and Understanding garbage collection in .net
On the other hand, many people do not recommend using GC.Collect. See What's Wrong About Using GC.Collect ()?
In my experience, killing a process is the fastest and easiest way to make sure Excel is gone. My code kills only the exact process that it launches, none other. I close all open books, Close the application and release the xlApp object. Finally, I check if the process is alive, and if so, 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 saw how many people say that killing the process is bad, but I have not seen any qualitative answers to the question why. Especially after the files are closed, Excel is gone, and we will only kill the exact process that we started. My question is what are the potential problems with killing the Excel process. Does it harm work? Will it harm Excel?
Many will also say that with good coding, I donโt need to kill the process. Maybe, but this does not answer the question "Why is it bad to kill the process?" After closing the files, exit Excel and free the objects; why would it be bad to just make sure the process is gone?
Edit: also what remains after exiting excel? If Excel was visible, it usually closes normally, disappears from the view and from the taskbar. And indeed, Excel has really gone away or failed to do so. It seems to me that Excel really stopped working and we only have an empty working shell. Can anyone comment on this?
Edit: I am interested to note that GC (aka Garbage Collection) through GC.Collect () GC.WaitForPendingFinalizers () will actually free the process shell remaining after exiting Excel. Does this support my assumption that the empty shell of the process is indeed garbage after all?
Edit: just found a great site on the issue: 50 ways to kill Excel