 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Benny-D
Joined: 29 Feb 2008 Posts: 865
|
Posted: Mon Jan 18, 2010 1:20 pm Post subject: Please, explain the workings of #persistent to me |
|
|
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 |
|
|
| Back to top |
|
 |
tonne
Joined: 06 Jun 2006 Posts: 1651 Location: Denmark
|
Posted: Mon Jan 18, 2010 1:50 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Kellianjaxon
Joined: 04 Jan 2008 Posts: 102
|
Posted: Mon Jan 18, 2010 1:57 pm Post subject: Re: Please, explain the workings of #persistent to me |
|
|
| 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. |
|
| Back to top |
|
 |
garry
Joined: 19 Apr 2005 Posts: 2212 Location: switzerland
|
Posted: Mon Jan 18, 2010 11:22 pm Post subject: |
|
|
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
|
|
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Tue Jan 19, 2010 1:12 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Benny-D
Joined: 29 Feb 2008 Posts: 865
|
Posted: Tue Jan 19, 2010 1:18 am Post subject: |
|
|
| Kellianjaxon, thank you very much for very clear explanation and for the vivid example!!! |
|
| Back to top |
|
 |
Benny-D
Joined: 29 Feb 2008 Posts: 865
|
Posted: Tue Jan 19, 2010 1:20 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|