0

So with _excel = New Excel.Application if im not using _excel.Quit and FinalReleaseComObject(_excel), when my application closes, the excel process will close too.

On the other hand when I use both cleanup methods, excel process will stay until my application closes.

Im using Windows 10 and Visual Studio 22

So has cleaning up unmanaged ressources become out of date?

  • How did you verify that Excel is no longer running? In Task Manager, did you look under "Background processes"? – Tu deschizi eu inchid Feb 23 '23 at 15:35
  • yes, also in details the EXCEL.EXE disappears –  Feb 23 '23 at 15:39
  • Have you tried using the method shown in this answer of mine: [Open file in a specific directory](https://stackoverflow.com/a/50446976/1115360)? – Andrew Morton Feb 23 '23 at 17:44
  • no i have not used `GC.Collect`, as it is not supposed to be used in working environment. But that is in fact the problem, the finalizer is called by the GC not by the Disposing process, so that most probably why it doesnt close excel immediately after disposing it, because the GC didnt run already. However it doesnt explain why excel closes even when its not cleaned up correctly –  Feb 23 '23 at 18:13
  • 1
    From my experience, this type of behaviour only seems to occur while debugging from VS. In production, it actually works as expected and processes are terminated when expected. For me, it's one reason why I avoid office interops if there is any possible way – Hursey Feb 23 '23 at 21:31

1 Answers1

0

The Office COM interops are being notoriously bad here... Normally what you should be doing is dispose of every object in the inverse order that they were created. For example, first you create Excel.Application, but then I imagine you also have references to workbooks and sheets. If you call .Quit before you first dispose references to the worksheets and close the workbook, the process stays in memory. You shouldn't have to call FinalReleaseComObject, in fact if you still have references not disposed, it may or may not crash the process...

Drunken Code Monkey
  • 1,796
  • 1
  • 14
  • 18
  • no this is all the COM associated code im using. No workbooks, sheets so far. Maybe excel.Quit can make FinalReleaseComObject redundant, I dont know. However i know it was usually used for all references you created. Not matter if im using quit or not i find opposite of expected behavior –  Feb 23 '23 at 15:42