Excel Instance not cleaned in Task Manager (Interop)

V

vinuthan

The following approach to code solved the problem

1) set the object to null when declraing all the object
Example:
Excel.Application xlApp = null;
Excel.Workbooks xlWorkBooks = null;
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;

2) Instantiate object properly. Expecially create the Workbooks object and
then the Workbook object. Earlier i created the workbook directly bu using
Excel.WorkBooks.Add
Now it looks like
xlApp = new Excel.ApplicationClass();
xlWorkBooks = xlApp.Workbooks;
xlWorkBook = xlWorkBooks.Add(misValue);
xlWorkSheet = new Microsoft.Office.Interop.Excel.Worksheet();
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Add

(misValue, misValue, misValue, misValue);

3) Release the com objects and set them to null.
NOTE: Each of the com objects created need to be released

Done by adding the Finally code

finally
{

// Destroy objects
if (xlApp != null)
{
xlWorkBook.Close(false, misValue, misValue);
xlWorkBooks.Close();
xlApp.Quit();
if (xlWorkSheet != null)
while
(System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet) != 0) {
}
//if (xlWorkSheets != null)
// while
(System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheets) != 0)
{ }
if (xlWorkBook != null)
while
(System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) != 0) { }
if (xlWorkBooks != null)
while
(System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBooks) != 0) {
}
if (xlApp != null)
while
(System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) != 0) { }

xlWorkSheet = null;
//xlWorkSheets = null;
xlWorkBook = null;
xlWorkBooks = null;
xlApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}


The above considerations solved the problem
The excel instance mainly was not getting deleted as the count was not
getting decremented (not all objects were released) and was not 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top