Guaranteed COM object release?

Posted by Jurily on Stack Overflow See other posts from Stack Overflow or by Jurily
Published on 2010-05-15T15:17:45Z Indexed on 2010/05/15 15:24 UTC
Read the original article Hit count: 340

Filed under:
|
|

I wrote the following code under the assumption that Excel will die with Monkey:

class ExcelMonkey
{
    private static Excel.Application xl = new Excel.Application();

    public static bool parse(string filename)
    {

        if (filename.Contains("foo"))
        { 
            var workbook = xl.Workbooks.Open(filename);
            var sheet = workbook.Worksheets.get_Item(1);

            // do stuff

            return true;

        }

        return false;
    }
}

How do I make sure it does? Do I need to release workbook and sheet separately?

I want to have Excel around for the lifetime of the program, it's a huge performance improvement.

© Stack Overflow or respective owner

Related posts about c#4.0

Related posts about com