Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does excel remain open? [duplicate]

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?

like image 635
Andres A. Avatar asked Feb 26 '26 21:02

Andres A.


2 Answers

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();
like image 128
Vinko Vrsalovic Avatar answered Feb 28 '26 11:02

Vinko Vrsalovic


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.

like image 23
Crispy Avatar answered Feb 28 '26 11:02

Crispy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!