4

I am using Excel Interop. In the beginning of a method I got, I'm allocating a new instance of the application and at the end of it I'm trying to free it, but when I look at the TaskManager I can still see Excel open.

This is the code:

A class member: private Excel.Application _app;

The usage:

 public void MethodApp()
{
  _app = new Excel.Application();
  ....
  ....
  FreeApplicationResources();
}

private void FreeApplicationResources()
{
  _app.Quit();
  Marshal.ReleaseComObject(_app);
}

MethodApp can run several times and it opens instances at the same quantity as the number of times it's called. Why won't Excel close?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • bookmark this. http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – Sorceri Oct 30 '13 at 15:23
  • The best answer in that link says `Never use 2 dots with com objects`. That is so incorrect. It should be changed to `Avoid the use of 2 dots with com objects.` And the reason is very simple. If you know how 2 dot rules works and you know how to clean up then there is no problem at all in using 2 DOT Rule. I use it all the time... You might wanna see [THIS](http://www.siddharthrout.com/2012/08/06/vb-net-two-dot-rule-when-working-with-office-applications-2/) – Siddharth Rout Oct 30 '13 at 15:32
  • Also see [THIS LINK](http://stackoverflow.com/questions/11813758/adding-text-into-excel-sheet-by-using-c-sharp/11813927#11813927) At the end of the code I have a `private void releaseObject(object obj)` for releasing objects. Use that. – Siddharth Rout Oct 30 '13 at 15:55

1 Answers1

1

Try releasing any worksheets and workbooks used also in the order below:

Marshal.ReleaseComObject(_worksheet);
Marshal.ReleaseComObject(_workbook);
Marshal.ReleaseComObject(_app);
Matt F
  • 585
  • 1
  • 6
  • 20