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