2

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?

Community
  • 1
  • 1
Andres A.
  • 1,329
  • 2
  • 21
  • 37
  • But there the "correct answer" is not the accepted one, because it didn't work for the questioner. It doesn't even have a single vote (I'm now going to upvote it). – Vinko Vrsalovic Dec 10 '09 at 22:25

4 Answers4

8

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
  • 330,807
  • 53
  • 334
  • 373
2

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.

Crispy
  • 5,557
  • 3
  • 30
  • 35
  • This is a good idea indeed, even if it will probably not solve the problem. – Vinko Vrsalovic Dec 10 '09 at 21:51
  • 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. – Crispy Dec 10 '09 at 21:53
  • @Chris. How could this slipped my mind?! Making changes right away! thanks for the catch. – Andres A. Dec 11 '09 at 20:11
1

Try using

xlApp.Application.Quit();

instead of

xlApp.Quit();

I ran into exactly the same issue recently :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Jon thanks Jon for such a quick response. I tried your suggestion but the problem remains, any other ideas? – Andres A. Dec 10 '09 at 21:45
  • 1
    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 Dec 10 '09 at 21:45
  • @Vinko: I didn't, but I'm using .NET 4.0 - maybe that makes a difference? – Jon Skeet Dec 10 '09 at 21:49
  • (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 Dec 10 '09 at 21:50
0

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):

c# and excel automation - ending the running instance

Community
  • 1
  • 1
  • a) Why GC.Collect ends it (reliably and reproducibly)? b) What does "RPC server not found/running" have to do with this? – Vinko Vrsalovic Dec 10 '09 at 22:27
  • 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. –  Dec 11 '09 at 14:39
  • 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 Dec 11 '09 at 20:48
  • 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. –  Dec 11 '09 at 21:04