Timer wait with no interruptions

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Timer wait with no interruptions

Post by hemsith14_ » 24 Jan 2022, 18:50

Hey, I want to get a timer to wait, while not interrupting other timers.
The situations is:

Code: Select all

SetTimer, A
SetTimer, B

A:
If WinActive("window")
{
do something
}
; Wait for "window" to not be active, while not interrupting timer B.
; OR any other way to get the timer to keep detecting the active window, while not repeating the commands if it's active, and not interrupting timer B.
Return

B:
; Very similar to A.
Thanks for your help!

User avatar
boiler
Posts: 16929
Joined: 21 Dec 2014, 02:44

Re: Timer wait with no interruptions

Post by boiler » 24 Jan 2022, 18:59

You can’t have more than one timer wait and then have either act as soon as it’s done waiting. That’s the exact reason I said to keep checking using WinActive() in your other thread. You are basically trying to find different ways to ask the same question which comes down to requiring multi-threading, which standard AHK does not do.

If you want more than one thread to operate in parallel so you can have two “waits” occur simultaneously, you can either run multiple scripts or use AHK_H.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Timer wait with no interruptions

Post by hemsith14_ » 24 Jan 2022, 19:04

I use (WinActive) in both of them.
The problem is that it keeps firing the commands on repeat if the window is active.
Maybe it can be done not by "waiting", but either way, I need those commands to fire only once their window is getting activated.

As I wrote, maybe there are other way to do it?

User avatar
boiler
Posts: 16929
Joined: 21 Dec 2014, 02:44

Re: Timer wait with no interruptions

Post by boiler » 24 Jan 2022, 19:10

I see now in your other thread you asked a similar question that somehow I missed. You can set a variable as a "flag" that says it was found to be active, and now only check if it's flag has been set to not found to be active. Something like this:

Code: Select all

ActiveA := 0
ActiveB := 0

SetTimer, A
SetTimer, B

A:
If WinActive("window") && !ActiveA
{
	ActiveA := 1
	do something
}
If !WinActive("window")
	ActiveA := 0
Return

B:
If WinActive("other window") && !ActiveB
{
	ActiveB := 1
	do something
}
If !WinActive("other window")
	ActiveB := 0
Return

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Timer wait with no interruptions

Post by hemsith14_ » 24 Jan 2022, 19:18

Awesome, I knew it was possible, and that something was missing.
Your solution is working perfectly and makes sense, thanks for your help.

P.S I'd appreciate if you could put descriptors with ";" in your script, so I can learn what's going on there.

User avatar
boiler
Posts: 16929
Joined: 21 Dec 2014, 02:44

Re: Timer wait with no interruptions

Post by boiler » 24 Jan 2022, 19:28

Sure. Glad to help. Sorry for not understanding your intent. Here is commented code. I commented on the A part, and of course, the B part operates under the same logic. Feel free to ask if it's still not clear.

Code: Select all

ActiveA := 0 ; set inital state to false (window A has not yet been acted on as the active window)
ActiveB := 0

SetTimer, A ; run subroutine A every 250 ms
SetTimer, B

A:
If WinActive("window") && !ActiveA ; execute this code block if window is active and the flag Active A is not set to true (i.e., it is 0 or false)
{
	ActiveA := 1 ; set the flag to true
	do something
}
If !WinActive("window") ; when the window is not found to be active...
	ActiveA := 0 ; set the flag to false so the other if statement's condition will allow it to act when the window becomes active
Return

B:
If WinActive("other window") && !ActiveB
{
	ActiveB := 1
	do something
}
If !WinActive("other window")
	ActiveB := 0
Return

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Timer wait with no interruptions

Post by hemsith14_ » 24 Jan 2022, 19:31

Great, love this logic! Have a good one. :clap:

Post Reply

Return to “Ask for Help (v1)”