How to terminate C# based Excel Application instance? -


i'm working on small code read excel files. i've linked com library excel 15, , using following code read excel files:

excelinstance = new microsoft.office.interop.excel.application(); workbook workbook = excelinstance.workbooks.open(@excelopenfiledialog.filename); worksheet worksheet = workbook.worksheets[1]; range usedrange = worksheet.usedrange; int lastusedrow = usedrange.row + usedrange.rows.count - 1; 

the problem don't know how terminate excel instance once processing finished. excel application interface isn't disposable, there way ?

disposing excel com objects can bit finicky. make sure you're calling close on workbook when finished, , quit on instance of excel.application.

release objects calling finalreleasecomobject(), , redundancy, explicitly null com objects:

runtime.interopservices.marshal.finalreleasecomobject(sheet); runtime.interopservices.marshal.finalreleasecomobject(workbook); runtime.interopservices.marshal.finalreleasecomobject(app);   sheet = null; workbook = null; app = null; 

lastly, if buggers still don't seem go away after calling appropriate disposal methods, explicitly releasing them, , nulling references objects , call garbage collector:

gc.collect(); gc.waitforpendingfinalizers(); 

and it.

cheers.

edit: also, sure in exception handler if there's chance code bomb while you're using interop objects, or because of them. you'll leave zombie processes if parent terminates.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -