PDA

View Full Version : Prevent a routine from being executed more than once



jlogan02
2017-12-06, 05:27 PM
How do I prevent a routine from running more than once?

J. Logan
ACAD 2016

GHarvey
2017-12-06, 05:45 PM
I don't know whether there are particular circumstances you are trying to deal with, but the simplest way I can think of is to have the function undefine itself once it is done. A very basic example:


(defun c:test ( )
(princ "\nTesting...")
(setq c:test nil)
(princ)
); defun

rkmcswain
2017-12-06, 08:24 PM
More than once what?

Per drawing, per session of AutoCAD, per machine per day, per machine per week? Etc., etc.

If it was per drawing, you could have the routine write something to the drawing database (xdata perhaps), and then have the routine check for the existence of this data prior to executing.
If it was per machine, forever - then you could write something to the registry, and have the routine check for it, etc.

Example:
I have some code that only runs once a week.
It checks the registry for an entry where a previous execution of this code writes the date/time
If the current date/time is more than 168 hours since the recorded time, then the routine executes the remainder of the code.

jlogan02
2017-12-07, 01:04 AM
More than once what?

Per drawing, per session of AutoCAD, per machine per day, per machine per week? Etc., etc.

If it was per drawing, you could have the routine write something to the drawing database (xdata perhaps), and then have the routine check for the existence of this data prior to executing.
If it was per machine, forever - then you could write something to the registry, and have the routine check for it, etc.

Example:
I have some code that only runs once a week.
It checks the registry for an entry where a previous execution of this code writes the date/time
If the current date/time is more than 168 hours since the recorded time, then the routine executes the remainder of the code.

Per drawing. The xdata approach makes sense. I'm being a bit lazy I guess. The routine moves some entities while doing other stuff. If the user hits the button again it moves the entities again. I suppose I should fix that.

BIG-AL
2017-12-08, 10:54 PM
Just set a variable once done



(if (= have _I_done_it "Y")
(princ "done")
(progn
(setq have _I_done_it "Y")
(do your thing)
))