Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Proper Way to Close Multiple Excel Objects With Excel Process Destroyed
    primarykey
    data
    text
    <p>I have a C# console program. When it runs, it instantiates some Excel objects such as: (openExcelO). </p> <p>When the program is done running, I have a method to close down Excel (closeExcel) and that is supposed to properly clean up the Excel process. In task manager I can see the remnants of Excel that remain. The shut down routines were cobbled together from internet examples:</p> <pre><code> private void openExcelO (string dir) { try { xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(dir + "PortfolioOptimization5.xlsm", 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlMarkowitz = (Excel.Worksheet)xlWorkBook.Sheets["Markowitz"]; xlWeights = (Excel.Worksheet)xlWorkBook.Sheets["Weights"]; } catch (Exception ex) { ReportError("PortfolioOptimization", "openExcel", ex.ToString()); } } private void closeExcel () { object misValue = System.Reflection.Missing.Value; try { if (xlWeights != null) releaseObject(xlWeights); if (xlMarkowitz != null) releaseObject(xlMarkowitz); xlWorkBook.Close(false, misValue, misValue); if (xlApp != null) xlApp.Quit(); if (xlWorkBook != null) releaseObject(xlWorkBook); if (xlApp != null) releaseObject(xlApp); } catch (Exception ex) { ReportError("PortfolioOptimization", "closeExcel", ex.ToString()); } } private void releaseObject (object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; ReportError("PortfolioOptimization", "releaseObject", "Unable to release object " + ex.ToString()); } finally { GC.Collect(); } } </code></pre> <p>How can I change the shutdown routine to properly shut down and clean up the Excel objects so there are no lingering Excel processes in the Task Manager?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload