ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel Instance not cleaned in Task Manager (Interop) (https://www.excelbanter.com/excel-programming/436321-excel-instance-not-cleaned-task-manager-interop.html)

vinuthan

Excel Instance not cleaned in Task Manager (Interop)
 
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.ReleaseCom Object(xlWorkSheet) != 0) {
}
//if (xlWorkSheets != null)
// while
(System.Runtime.InteropServices.Marshal.ReleaseCom Object(xlWorkSheets) != 0)
{ }
if (xlWorkBook != null)
while
(System.Runtime.InteropServices.Marshal.ReleaseCom Object(xlWorkBook) != 0) { }
if (xlWorkBooks != null)
while
(System.Runtime.InteropServices.Marshal.ReleaseCom Object(xlWorkBooks) != 0) {
}
if (xlApp != null)
while
(System.Runtime.InteropServices.Marshal.ReleaseCom Object(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


All times are GMT +1. The time now is 12:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com