0

I use Excel as Com object in my application. There is code example

    public void LoadData()
    {
        LogHandler logHandler = new LogHandler(Path.GetDirectoryName(FileName) + "\\logfile.log");
        string OKATO = GetOKATOFromFileName();
        int SubjectId;
        if (!int.TryParse(OKATO, out SubjectId))
        {
            logHandler.WriteLogStr("Ошибка определения ОКАТО из имени файла (" + FileName  + ")");
            return;
        }
        int CL;
        DateTime FDate = GetDateFromFileName(out CL);
        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(FileName,
            0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
            true, false, 0, true, false, false);
        try
        {
            Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            for (int i = 1; i <= excelSheets.Count; i++)
            {
                Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(i);
                LoadDataFromSheet(excelWorksheet, SubjectId, CL, FDate);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet);
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheets);
        }
        catch (Exception le)
        {
            logHandler.WriteLogStr(le.Message);
        }
        finally
        {
            excelWorkbook.Close(0);
            excelApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
        }

    }

After exiting from void I expect that Excel will disappear from memory in case of this code

        finally
        {
            excelWorkbook.Close(0);
            excelApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
        }

but it doesn't. Until program closed Excel exists in memory. It quits only after closing my application.

How can I close Excel process before I close my application.

p.s. I've read this topic Closing Excel Application Process in C# after Data Access but none of the suggestions work for me

enter image description here

Community
  • 1
  • 1
Alex Zhulin
  • 1,239
  • 2
  • 22
  • 42
  • Does this help: http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – danish Nov 17 '14 at 04:08
  • have you used this line `System.Runtime.InteropServices.Marshal.ReleaseComObject(yourcomobjects);` for all of the objects you're creating example excelWorkbook, excelApp, etc.. – MethodMan Nov 17 '14 at 04:09
  • I've added Marshal.ReleaseComObject for all of my com variables, but Excel still in memory. – Alex Zhulin Nov 17 '14 at 04:16
  • Changed in my code sample in question – Alex Zhulin Nov 17 '14 at 04:18
  • If none is working, use kill process. Check this URL: http://stackoverflow.com/questions/17777545/closing-excel-application-process-in-c-sharp-after-data-access – Paresh J Nov 17 '14 at 04:37
  • Get rid of the `try`/`catch`/`finally` block. Unless you are expecting to catch a specific exception then "catch all" exception handling is more likely to cause errors than prevent them. – Enigmativity Nov 17 '14 at 04:45

1 Answers1

0

When you done disposing the object, use

GC.Collect();

This is how I dispose my Excel object

        private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }
Andrew
  • 7,619
  • 13
  • 63
  • 117