I use some objects like workbook, worksheet, app for Excel tasks. After I am done with Excel, I try releasing them by the code below. After all, I still see EXCEL.EXE at Task Manager. Why doesn't it release completely?
My class for Excel tasks:
Excel._Application app = new Excel.Application();
Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Excel._Worksheet worksheet = null;
app.Visible = true;
worksheet.Cells[1, 1] = "test string";
workbook.SaveAs("C:\testfile.xlsx");
object misValue = System.Reflection.Missing.Value;
workbook.Close(true, misValue, misValue);
app.Quit();
releaseObject(worksheet2);
releaseObject(workbook);
releaseObject(app);
releaseObject class:
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
EXCEL.EXE is still at Task Manager: