Killing EXCEL.exe Process from C# in a Windows Service -


i have windows service opens excel spreadsheet via microsoft.office.interop.excel.application object.

application xlapp = new application(); workbook workbook = xlapp.workbooks.open(filename, 2, false); ... ... workbook.close(); xlapp.quit(); 

i kill excel.exe process left running after done working workbook.

i've tried following no success...

// returns processid of 0 intptr processid; getwindowthreadprocessid(new intptr(xlapp.hwnd), out processid); process p = process.getprocessbyid(processid.toint32());    p.kill(); 

anyone have ideas how can via windows service?

properly closing open excel workbook , quitting app extremely difficult. if can find links i'll post them, must clean references com object create. includes odbcconnections (data connections), worksheets, workbooks, , excel application. combination got work involved garbage collection , system.runtime.interopservices.marshal object:

// garbage collecting gc.collect(); gc.waitforpendingfinalizers(); // clean references com objects // per above, you're using workbook , excel application instance, release them: workbook.close(false, missing.value, missing.value); xlapp.quit(); marshal.finalreleasecomobject(workbook); marshal.finalreleasecomobject(xlapp); 

like mentioned, looping through , killing each excel process not idea, since if you're running windows app may close excel on user, or in service close instance of excel running via other program.

edit: see this question more info.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -