Possible Duplicate:
How to properly clean up Excel interop objects in C#
I've this function that I use to calculate the linear trend of some data:
private string Trend(object conocido_y, object conocido_x, object nueva_matriz_x)
{
string result = String.Empty;
try {
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
result = ((Array)xlApp.WorksheetFunction.Trend(conocido_y, conocido_x, nueva_matriz_x, true)).GetValue(1).ToString();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
catch (System.Runtime.InteropServices.COMException ex) {
DError.ReportarError(ex, false);
}
catch (Exception ex) {
DError.ReportarError(ex);
}
return result;
}
the results are fine but the excel app doesn't close, if I open the task manager the process is still running, why?
I remember having seen that, after ReleaseComObject(), a forced GC pass is due for the object to be released and excel to finally die.
Also, I don't see it in that snippet, but you have to ReleaseComObject() in any sheet or other Excel object you might have gotten a handle on (is result such a thing?).
ReleaseComObject(result);
app.Aplication.Quit();
ReleaseComObject(app);
GC.Collect();
Is your function creating an error? If so the Quit() is never reached. You may want to put the Quit and ReleaseComObject in a finally block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With