Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

loops


  • Please log in to reply
13 replies to this topic
degarb
  • Members
  • 308 posts
  • Last active: Jan 22 2012 06:05 PM
  • Joined: 14 Feb 2007
I wrote a monitor log script, but also wish to monitor a website--two perpetual loops within/using same exe (compiled).

Can AHK do two loops at once? Only with settimer? (I think I used goto in one loop.)

degarb
  • Members
  • 308 posts
  • Last active: Jan 22 2012 06:05 PM
  • Joined: 14 Feb 2007
I always assumed not. In past to avoid bloated distro, I would create an engine (resource edited autohotkey.exe) and pass off scripts to make multiple threads.

Problems is this means more files in the directory to confuse user, loss of hiding code, and need for a zipped distro or extractor.

toralf
  • Moderators
  • 4028 posts
  • Last active: Dec 27 2013 01:43 AM
  • Joined: 31 Jan 2005
Two loops are possible, you have to start two threads (SetTimer), or the script is calling itself again (maybe with a command line parameter, so that it knows it is the second call). Either way, you have only one file.

degarb
  • Members
  • 308 posts
  • Last active: Jan 22 2012 06:05 PM
  • Joined: 14 Feb 2007
Thanks, I think I understand. (anyone with ex.?)


>Two loops are possible, you have to >start two threads (SetTimer),

So any set timer should start a loop on startup of script? So several set timers start several loops at one time?

>or the script is calling itself >again (maybe with a command line >parameter, so that it knows it is >the second call).

What kind of parameter?
A script to run self. Would not this create endless opening same new scripts at exponential rate [1>2>4>8>16>32>64>128>etc], and crash your machine?

ManaUser
  • Members
  • 1121 posts
  • Last active: Mar 03 2011 09:08 PM
  • Joined: 24 May 2007
I was going to give an example... only it doesn't work.
SetTimer SecondLoop, -1

X := 0
Y := 1

Loop
{
   X += 1
   ToolTip X = %X%, 10, 20, 1
   Sleep 1000
}

SecondLoop:
Loop
{
   Y *= 2
   ToolTip Y = %Y%, 10, 40, 2
   Sleep 1000
}
I guess script threads don't interact the way I thought.

Making the compiled script call itself might work. It would just need to to let the second copy know not to call any more. A command line parameter would probably be the simplest way.

tonne
  • Members
  • 1654 posts
  • Last active: Apr 10 2014 04:12 PM
  • Joined: 06 Jun 2006
Why mix timers and loops?

SetTimer SecondLoop, 1000, -1 
SetTimer FirstLoop, 1000, 0

X := 0 
Y := 1 

return

FirstLoop:
   X += 1 
   ToolTip X = %X%, 10, 20, 1 
return

SecondLoop: 
   Y *= 2 
   ToolTip Y = %Y%, 10, 40, 2 
return


polyethene
  • Administrators
  • 5511 posts
  • Last active: Yesterday, 11:38 PM
  • Joined: 26 Oct 2012

Why mix timers and loops?

You may want real simultaneous loops for A_Index, Break, Continue etc. and ManaUser's example shows how you can do that.

tonne
  • Members
  • 1654 posts
  • Last active: Apr 10 2014 04:12 PM
  • Joined: 06 Jun 2006
only it doesn't work...

When the timed thread with a loop is started the x-loop never continues.

polyethene
  • Administrators
  • 5511 posts
  • Last active: Yesterday, 11:38 PM
  • Joined: 26 Oct 2012

When the timed thread with a loop is started the x-loop never continues.

It does for me. You may need to update your version of AutoHotkey since the '-1' option on SetTimer is a relatively new feature.

daniel2
  • Guests
  • Last active:
  • Joined: --
It doesn't for me either; I was actually toying around w/ this the other day-- & I evan tried adjusting priority level w/ no success..
#Persistent
X := 0 , Y := 1
SetTimer, FirstLoop, -1, 1
SetTimer, SecondLoop, -1, 1
return

^t::Msgbox, Hotkeys don't seem to launch thread either...

FirstLoop:
Loop  {
   X += 1
   ToolTip X = %X%, 10, 20, 1
   Sleep 1000
}

SecondLoop:
Loop  {
   Y *= 2
   ToolTip Y = %Y%, 10, 40, 2
   Sleep 1000
}
It seems like the thread that takes charge is the one w/ the highest priority or if same priority-- the most recent called. I also noticed that hotkeys don't launch thread as expected either :?
I wasn't going to post since I don't regularly create infinate loops.. but since I saw someone talking about it.........

tonne
  • Members
  • 1654 posts
  • Last active: Apr 10 2014 04:12 PM
  • Joined: 06 Jun 2006
Just installed 1.0.47.01 (from 1.0.47.00) and still it doesn't work.
X is counted to 1 and Y is doubled until script is stopped.

ManaUser
  • Members
  • 1121 posts
  • Last active: Mar 03 2011 09:08 PM
  • Joined: 24 May 2007
All I can figure is that AutoHotkey "threads" don't work how you would expect. It seems one thread always has exclusive control, until either it ends, or is interupted. AHK never does time sliceing or anything like that as far as I can see.

But tonne does have a point. What you're trying to do can almost surely be accomplished without two actuall loops running at the same time. It may just take more work to set it up.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Feb 27 2014 01:49 PM
  • Joined: 30 Aug 2005
AHK is not multithreaded. Try using a positive timer value, and they won't step on each other.

#Persistent
X := 0 , Y := 1
SetTimer, FirstLoop, 1000
SetTimer, SecondLoop, 1000
return

^t::Msgbox, Hotkeys don't seem to launch thread either...

FirstLoop:
   X += 1
   ToolTip X = %X%, 10, 20, 1
Return

SecondLoop:
   Y *= 2
   ToolTip Y = %Y%, 10, 40, 2
Return


daniel2
  • Guests
  • Last active:
  • Joined: --

Why mix timers and loops?

You may want real simultaneous loops for A_Index, Break, Continue etc. and ManaUser's example shows how you can do that.

tonne also gave a regular timer example (not using loop..). My point was that if you do need to use a loop in a thread (or pseudo-thread.. whatever you want to call them..) they seem to conflict w/ other threads.