See the top rated post in this thread. Click here

Results 1 to 5 of 5

Thread: Prevent a routine from being executed more than once

  1. #1
    Member
    Join Date
    2001-08
    Posts
    24
    Login to Give a bone
    0

    Default Prevent a routine from being executed more than once

    How do I prevent a routine from running more than once?

    J. Logan
    ACAD 2016

  2. #2
    100 Club
    Join Date
    2000-11
    Location
    Ontario, Canada
    Posts
    116
    Login to Give a bone
    0

    Default Re: Prevent a routine from being executed more than once

    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:

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

  3. #3
    Administrator rkmcswain's Avatar
    Join Date
    2004-09
    Location
    Earth
    Posts
    9,805
    Login to Give a bone
    0

    Default Re: Prevent a routine from being executed more than once

    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.
    R.K. McSwain | CAD Panacea |

  4. #4
    Member
    Join Date
    2001-08
    Posts
    24
    Login to Give a bone
    1

    Default Re: Prevent a routine from being executed more than once

    Quote Originally Posted by rkmcswain View Post
    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.

  5. #5
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    561
    Login to Give a bone
    0

    Default Re: Prevent a routine from being executed more than once

    Just set a variable once done

    Code:
    (if (= have _I_done_it "Y")
    (princ "done")
    (progn
    (setq have _I_done_it "Y")
    (do your thing)
    ))
    Last edited by rkmcswain; 2017-12-11 at 12:50 PM. Reason: fix [CODE] tags

Similar Threads

  1. Help with a lisp routine to add a 12" line to this routine
    By Orbytal.edge341183 in forum AutoLISP
    Replies: 3
    Last Post: 2012-11-14, 10:33 PM
  2. Replies: 9
    Last Post: 2007-06-13, 01:22 PM
  3. Replies: 2
    Last Post: 2007-04-20, 09:51 AM
  4. Replies: 2
    Last Post: 2005-12-19, 06:19 PM
  5. Replies: 1
    Last Post: 2005-11-15, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •