0

I am having an issue with the EXCEL app staying open in the task manager, even after I clean up (or think I am cleaning up) via code. Can anyone point out what I am missing from the snippet of code below:

private void btnBrowseSquid_Click(object sender, EventArgs e)

        //starting up and defining the Excel references
        Excel.Workbook squidBook = null;
        Excel.Application excelApp = null;
        Excel.Worksheet squidSheet = null;
        string squidFileName = txtSquid.Text;

        //Reading the information from the "Filenames" tab of the SQUID file into the Windows Form
        excelApp = new Excel.Application();
        excelApp.Visible = true;
        squidBook = excelApp.Workbooks.Open(squidFileName);
        squidSheet = squidBook.Sheets["Filenames"];

        //do stuff here with the excel file

        //close the open Excel App
        squidBook.Close();
        excelApp.Quit();
        Marshal.FinalReleaseComObject(squidSheet);
        Marshal.FinalReleaseComObject(squidBook);

    }
Darw1n34
  • 312
  • 7
  • 24
  • You probably have an old process from a failed attempt where your code didn't reach the end.... – Daniel Möller Jul 07 '15 at 16:26
  • 1
    Manual memory management never works, there is a WorkSheets interface reference that is completely invisible in your code. And inevitably you forgot to release it. Simply stop writing code like this, there is no point to it, the garbage collector never gets this wrong. [Read this](http://stackoverflow.com/a/25135685/17034). – Hans Passant Jul 07 '15 at 16:30

4 Answers4

0

do the following also :

Worksheets sheets = excelApp.Worksheets; 
Marshal.ReleaseComObject(squidSheet);
Marshal.ReleaseComObject(squidBook );
Marshal.ReleaseComObject(sheets);
squidSheet = null;
squidBook = null;
sheets = null;
GC.Collect();
GC.WaitForPendingFinalizers();

You have to release the sheets because you are holding a reference to it by doing this statement :

squidSheet = squidBook.Sheets["Filenames"];
Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
  • I added these but sadly I am still in the same boat. One EXCEL instance remains open – Darw1n34 Jul 07 '15 at 16:36
  • make sure that your other references are als put on null. – Philip Stuyck Jul 07 '15 at 16:48
  • I didn't downvote, but I recommend taking a look at Hans' comment on the original question for clarification. – Equalsk Jul 07 '15 at 16:53
  • Hans comment does not help the OP. I honestly don't understand why people downvote when one is trying to help. OP, added sheets as well, because you are holding a reference to that as well. Please try, if it still does not work, I'll delete this answer because then I don't know what other reference you are holding. – Philip Stuyck Jul 07 '15 at 16:54
  • Hans answer explains why manual memory management is a common misconception and why releasing objects like this is unnecessary. It explains how to correctly release garbage and that the OP's observed behaviour is likely due to a debugging quirk. If I've misunderstood then I definitely want to be corrected, I'm always happy to learn a new way to master the beast of Office Interop :-) – Equalsk Jul 07 '15 at 17:04
  • @Equalsk you are right it is in the read this, but the point is that the GC.Collect and waitforpendingfinalizers are needed too. Which means this answer is at least helpful. I am curious that with my last changes, the OP's problem is solved. Which is the only thing I am trying to do ;-) – Philip Stuyck Jul 07 '15 at 17:05
0

You probably have an old process from a failed attempt where your code didn't reach the end....

Try finishing that before running the code. (make sure you don't have other excel applications open).

When you are sure your code never fails, run it and see the application vanish from the proccess list.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • I am starting with no EXCEL instances open (verified in Task manager) before I launch the program. Even with the GC codes added as per Philip it remains open, and a royal pain...meh – Darw1n34 Jul 07 '15 at 16:35
  • Are you sure you don't open any other new application during `do stuff`? This should really be working fine. – Daniel Möller Jul 07 '15 at 16:39
0

Working with Office Interop can be tricky because there are a lot of bugs in the cleanup code. Have you tried reducing the code to the bare minimum and expand outwards until the issue reproduces (e.g. create the app, quit the app; does Excel.exe hang out? create the app, load the workbook, close the workbook, quit the app; does Excel.exe hang out? ...)?

All that said, the last time I had this problem (with Outlook) the root cause was because the default interop layer didn't release the event hooks properly - so I had to do that explicitly. Prepare for some frustration, but with some experimentation you should be able to run it down.

0

I was able to follow @Philip advice and nulled the ExcelApp too then Released the COM object like below, and it worked. Thanks for your help.

 //close the open Excel App
        squidBook.Close();
        excelApp.Quit();
        Marshal.FinalReleaseComObject(squidSheet);
        Marshal.FinalReleaseComObject(squidBook);
        Marshal.FinalReleaseComObject(excelApp);
        excelApp = null;
        squidSheet = null;
        squidBook = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
Darw1n34
  • 312
  • 7
  • 24
  • Glad I could help. You can indicate this as aswer. I' would be grathfull if you mark my answer as helpful if you not already did ;-) – Philip Stuyck Jul 07 '15 at 17:17