Besides the vlax-release-object method is there a way that I can ENSURE that the background process (specifically, EXCEL.EXE) is closed in my routine?
The need is because if I open excel, make some changes or even not make any changes.....close excel....the background process for EXCEL.EXE is still running. This is via double-clicking method of opening excel....I would handle it via the vlax commands in my routine but I want to have a line that will pre-emptively close the background process so that i can start it right back up again.
Closest i've found is lee mac's running processes function....combined with a call to (member), but perhaps there is a better way?
For reference, credits to LeeMac
;(defun runningprocesses ( / qry rtn srv wmi ) ; (if (setq wmi (vlax-create-object "wbemscripting.swbemlocator")) ; (progn ; (setq rtn ; (vl-catch-all-apply ; '(lambda ( / lst ) ; (setq srv (vlax-invoke wmi 'connectserver) ; qry (vlax-invoke srv 'execquery "Select * from Win32_Process") ; ) ; (vlax-for itm qry ; (vlax-for prp (vlax-get itm 'properties_) ; (if (= "name" (strcase (vlax-get prp 'name) t)) ; (setq lst (cons (vlax-get prp 'value) lst)) ; ) ; ) ; ) ; lst ; ) ; ) ; ) ; (foreach obj (list qry srv wmi) ; (if (= 'vla-object (type obj)) ; (vlax-release-object obj) ; ) ; ) ; (if (vl-catch-all-error-p rtn) ; (prompt (vl-catch-all-error-message rtn)) ; rtn ; ) ; ) ; ) ;)
Thanks in advance!
Solved! Go to Solution.
Solved by Lee_Mac. Go to Solution.
It sounds like a bad idea to try to kill a running application, assuming that it might only be a background process.
In general, when using lisp, if you don't want to duplicate running apps, try using vlax-get-or-create-object rather than vlax-create-object. This keeps you from spawning more instances of Excel.
If you create a new excel.application object, then you need to keep up with all the references to the object or localize them. When they go out of scope, you should be able to run the garbage collector to close the external application. (gc)
I try to release objects in the reverse order in which they were create AND localize them. Checking the return value of the vlax-release-object can help give you clues about whether there are still references to the object.
HTH
One more item: The help files for vlax-release-object state: "If an object-associated application does not close after calling the (gc) function, the (vlax-release-object) function was not called for all objects outside AutoCAD"
Are you calling the quit method of the Excel Application object before releasing the various vla-objects?
Lee- I have not tried that, whoops, should have looked into that before posting here.
Should take care of it.
There is quite a bit to keep up with in manipulating external programs via lisp I must say!
One would think there could be easier ways, but I imagine there are reasons it's as complicated as is it.
(vlax-get-or-create-object)
and then maybe (vlax-close-object) that'd close the instance, background process, and all pointers but meh, I just need to do more studying and use what's available!
Thanks
If I didn't appreciate the knowledge, talent, and support of every user in this thread so much I would ask, "WHY are we still using AutoLISP/VisualLISP and not VB.NET!?"