How to properly clean up Excel interop objects in C#

Posted by HAdes on Stack Overflow See other posts from Stack Overflow or by HAdes
Published on 2008-10-01T17:18:43Z Indexed on 2011/01/10 1:54 UTC
Read the original article Hit count: 687

Filed under:
|
|
|

I'm using the Excel interop in C# (ApplicationClass) and have placed the following code in my finally clause:

while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet) != 0) { }
excelSheet = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Although, this kind of works the Excel.exe process is still in the background even after I close Excel. It is only released once my application is manually closed.

Anyone realize what I am doing wrong, or has an alternative to ensure interop objects are properly disposed of.

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about excel