views:

205

answers:

4

Hello all, 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?

A: 

Try using

xlApp.Application.Quit();

instead of

xlApp.Quit();

I ran into exactly the same issue recently :)

Jon Skeet
@Jon thanks Jon for such a quick response. I tried your suggestion but the problem remains, any other ideas?
Unlimited071
Didn't you have to force GC and the like? I recall that even Application.Quit() is not enough (I don't have that code here, so I may be mistaken.)
Vinko Vrsalovic
@Vinko: I didn't, but I'm using .NET 4.0 - maybe that makes a difference?
Jon Skeet
(And I now can't reproduce it going wrong with just Quit instead of Application.Quit. I'm tempted to delete this answer...)
Jon Skeet
+4  A: 

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();
Vinko Vrsalovic
@Vinko Thanks Vinko that was it!
Unlimited071
+2  A: 

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.

Chris Persichetti
This is a good idea indeed, even if it will probably not solve the problem.
Vinko Vrsalovic
Yeah I just re-read your question and you said the results were ok, so probably not the problem. I have the same issue where Excel still shows up in task mgr. After a while they go away, so it's probably a GC thing.
Chris Persichetti
@Chris. How could this slipped my mind?! Making changes right away! thanks for the catch.
Unlimited071
A: 

Excel is a COM Automation Server.

Even though you call Application.Quit() and you Release the Reference to the COM object, the exe itself will not end. You will still be able to see it in task manager. Making further calls to Excel will use the running instance.

The Excel instance will exit after your application (thread, session, etc..) closes.

Ever get "RPC server not found/running" type COM errors? Now you know why.

See also (this has been asked many times on SO):

http://stackoverflow.com/questions/1826441/c-and-excel-automation-ending-the-running-instance/1826602#1826602

a) Why GC.Collect ends it (reliably and reproducibly)? b) What does "RPC server not found/running" have to do with this?
Vinko Vrsalovic
a. Why wouldn't it? b. It's an interesting piece of information applicable to COM servers like Excel. You could get this error were to make further calls to Excel after having closed it and not released references to it.
a) I'm saying that GC-ing it, without exiting the app, ends the Excel instance, which you are saying is only possible after your thread or app ends b) Okay, now that makes sense.
Vinko Vrsalovic
a) "which you are saying is only possible after your thread or app ends" - noop, that's not what I'm saying. If you ReleaseComObject, FinalReleaseComObject and *don't* GC, the Excel instance will exit after your application closes, which is presumable caused by an implicit GC, which has the added benefit of keeping the RPC Server running should you want to make more calls to it.