c# - Excel process not closing -


this question has answer here:

i've got c# program never closes excel process. finds number of instances string appears in range in excel. i've tried kinds of things, it's not working. there form calling method, shouldn't change why process isn't closing. i've looks @ suggestions hans passant, none working.

edit: tried things mentioned , still won't close. here's updated code. edit: tried whole process.kill() , works, seems bit of hack should work.

public class comparehelper {     // define variables     excel.application excelapp = null;     excel.workbooks wkbks = null;     excel.workbook wkbk = null;     excel.worksheet wksht = null;     dictionary<string, int> map = new dictionary<string, int>();      // compare columns     public void getcounts(string startrow, string endrow, string columnsin, system.windows.forms.textbox results, string excelfile)     {         results.text = "";          try         {             // create instance of microsoft excel , make invisible             excelapp = new excel.application();             excelapp.visible = false;              // open workbook , active worksheet             wkbks = excelapp.workbooks;             wkbk = wkbks.open(excelfile, type.missing, true);             wksht = wkbk.activesheet;             ...          }         catch         {             throw;         }                 {             gc.collect();             gc.waitforpendingfinalizers();              if (wksht != null)             {                 //wksht.delete();                 marshal.finalreleasecomobject(wksht);                 wksht = null;             }              if (wkbks != null)             {                 //wkbks.close();                 marshal.finalreleasecomobject(wkbks);                 wkbks = null;             }              if (wkbk != null)             {                 excelapp.displayalerts = false;                 wkbk.close(false, type.missing, type.missing);                 marshal.finalreleasecomobject(wkbk);                 wkbk = null;             }              if (excelapp != null)             {                 excelapp.quit();                 marshal.finalreleasecomobject(excelapp);                 excelapp = null;             }              gc.collect();             gc.waitforpendingfinalizers();             gc.collect();             gc.waitforpendingfinalizers();              /*             process[] processes = process.getprocessesbyname("excel");             foreach (process p in processes)             {                 p.kill();             }             */         }     } } 

here interesting knowledge base on subject of office apps staying open after .net app disconnects them.

office application not quit after automation visual studio .net client

the code examples in link (vb.net sorry). shows how correctly setup , tear down office app closes when you're finished it.

system.runtime.interopservices.marshal.finalreleasecomobject magic happens.

edit: need call finalreleasecomobject each excel object you've created.

if (excelworksheet1 != null) {     marshal.finalreleasecomobject(excelworksheet1);     excelworksheet1 = null; } if (excelworkbook != null) {     marshal.finalreleasecomobject(excelworkbook);     excelworkbook = null; } if (excelapp != null) {     marshal.finalreleasecomobject(excelapp);     excelapp = null; } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -