AutoHotkey Community

It is currently May 27th, 2012, 1:43 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: January 18th, 2010, 2:20 pm 
Offline

Joined: February 29th, 2008, 12:11 pm
Posts: 943
To me, the (#persistent) command is quite mysterious. I hardly
know what it is for and the example in the "Help" file doesn't
explain anything.

The "Help" file page on #persistent says:

"If this directive is present anywhere in the script, that script will
stay running after the auto-execute section (top part of the script)
completes.
"

Does that mean that my script below #persistent will be running
again, again and again like a loop?

Another thing that kind of scares me is this (from the same page
of the "Help" file):

"If this directive is added to an existing script, you might want to
change some or all occurrences of
Exit to be ExitApp. This is
because
Exit will not terminate a persistent script; it terminates
only the current thread.
"

Well, if it's only about changing Exits to ExitApps, then I can go
with it, but can it happen so that #persistent will damage or
influence somehow some other portions of script?

Why does the following script work similarly both with and
without the #persistent command in the first line?
Code:
#persistent
   $LButton::
   Loop
      {
      if not GetKeyState("LButton", "P")
         break
      GetKeyState, LButtonState, LButton, P
        If LButtonState = U
            break
       goto,A2
       }
   return


A2:
msgbox,,,Clicked LefButton,1
return

esc::exitapp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 18th, 2010, 2:50 pm 
Offline

Joined: June 6th, 2006, 3:19 pm
Posts: 1654
Location: Denmark
The #persistent command force the script to stay in memory in the exact same way your hotkey make it stay in memory.
Quote:
This is useful in cases where a script contains timers and/or custom menu items but not hotkeys, hotstrings, or any use of OnMessage() or Gui.

_________________
RegEx Powered Dynamic Hotstrings
COM
AutoHotkey 2


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 18th, 2010, 2:57 pm 
Offline

Joined: January 5th, 2008, 12:11 am
Posts: 102
Benny-D wrote:
Does that mean that my script below #persistent will be running
again, again and again like a loop?

No, it means scripts having no hotkeys, hotstrings, Onmessage() or Gui will not terminate after the auto-execute section completes.

Benny-D wrote:
Well, if it's only about changing Exits to ExitApps, then I can go
with it, but can it happen so that #persistent will damage or influence somehow some other portions of script?

Consider the following example which incorporates the usefulness of using #Persistent:

Code:
#Persistent
a = 0
Settimer, popup, 1000
Return

popup:
a++
Msgbox, This is the popup number %a%.
Return

If you comment out the first line you'll notice that the script reaches Return line at the end of the auto-execute section before the timer has had a single chance to run. Because there are no hotkeys, hotstrings, Onmessage() or Gui there's nothing to keep the script running which means the script will terminate the moment it has reached and processed the aforementioned Return line. The purpose of #Persistent is to prevent that from happening.

Benny-D wrote:
Why does the following script work similarly both with and without the #persistent command in the first line?

That's because you've got hotkey definitions in the script. Those definitions basically prevent the script from terminating at the end of the auto-execution. Meaning you won't benefit from using #Persistent in this case.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2010, 12:22 am 
Offline

Joined: April 19th, 2005, 10:26 am
Posts: 2249
Location: switzerland
thank you for the explanation, Kellianjaxon
also an example , needs persistent
Code:
#persistent
Splashimage,,M2 x290 y330  h70 CWred m9 fs10 zh0,Text2 ,text_Bold1,TITLE1
return

guiclose:
SplashImage, Off
exitapp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2010, 2:12 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
As Kellianjaxon hinted, it is necessary to understand what the Auto-execute Section is, to grasp the meaning of #Persistent. The first two paragraphs are most relevant.
AHK Help File wrote:
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first). This top portion of the script is referred to as the auto-execute section.

A script that is not persistent and that lacks hotkeys, hotstrings, OnMessage, and GUI will terminate after the auto-execute section has completed. Otherwise, it will stay running in an idle state, responding to events such as hotkeys, hotstrings, GUI events, custom menu items, and timers.
In my experience, the only useful purpose of #Persistent is to keep a timer running. If SetTimer was added to the list of commands that automatically make a script persistent, I think the directive could be eliminated entirely.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2010, 2:18 am 
Offline

Joined: February 29th, 2008, 12:11 pm
Posts: 943
Kellianjaxon, thank you very much for very clear explanation and for the vivid example!!!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2010, 2:20 am 
Offline

Joined: February 29th, 2008, 12:11 pm
Posts: 943
jaco0646 wrote:
In my experience, the only useful purpose of #Persistent is to keep a timer running. If SetTimer was added to the list of commands that automatically make a script persistent, I think the directive could be eliminated entirely.
I see, jaco0646, thank you. You're right, Auto-Execute section was something I didn't understand either.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], bobbysoon, Google Feedfetcher, JSLover, Tipsy3000, Yahoo [Bot] and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group