c# - Releasing Com Objects -
i having issue excel app staying open in task manager, after clean (or think cleaning up) via code. can point out missing snippet of code below:
private void btnbrowsesquid_click(object sender, eventargs e) //starting , defining excel references excel.workbook squidbook = null; excel.application excelapp = null; excel.worksheet squidsheet = null; string squidfilename = txtsquid.text; //reading information "filenames" tab of squid file windows form excelapp = new excel.application(); excelapp.visible = true; squidbook = excelapp.workbooks.open(squidfilename); squidsheet = squidbook.sheets["filenames"]; //do stuff here excel file //close open excel app squidbook.close(); excelapp.quit(); marshal.finalreleasecomobject(squidsheet); marshal.finalreleasecomobject(squidbook); }
do following :
worksheets sheets = excelapp.worksheets; marshal.releasecomobject(squidsheet); marshal.releasecomobject(squidbook ); marshal.releasecomobject(sheets); squidsheet = null; squidbook = null; sheets = null; gc.collect(); gc.waitforpendingfinalizers(); you have release sheets because holding reference doing statement :
squidsheet = squidbook.sheets["filenames"];
Comments
Post a Comment