Timer and menu Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Timer and menu

09 Apr 2021, 18:10

Code: Select all

#Persistent
#SingleInstance Force

Menu, MyMenu, Add, Label1
Menu, MyMenu, Add, Label2
Menu, MyMenu, Show

if (A_Args.1 = "Label1") {
    Gosub, Label1
	ExitApp
} else {
    Loop, % A_Args.Count()
    if IsLabel(A_Args[A_Index])
        Gosub, % A_Args[A_Index]
}
return

Label1:
    SetTimer, Timer, 500
return

Timer:
    DriveGet, status, Status, Z:\
    if (status = "Invalid")
        return
    SetTimer, Timer, Off
return

Label2:
    Gosub, Label1
	Run app.exe
ExitApp
When I pass Label2 as a parameter, why does the app try to run app.exe before Z:\ exists?
When I select Label1 from the menu, how can I exit the app as happens when I pass it as a parameter?
How can I exit the app if I click away from the menu?
User avatar
mikeyww
Posts: 26879
Joined: 09 Sep 2014, 18:38

Re: Timer and menu

09 Apr 2021, 19:04

Label2 starts the timer. Next, it runs app.exe, because that is the second command in your routine.

If you want the Label1 routine to ExitApp, then put that command into the routine.

To exit the app after showing the menu, add ExitApp after the Show command. A menu display will temporarily pause the script.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Timer and menu

09 Apr 2021, 19:24

mikeyww wrote:
09 Apr 2021, 19:04
Label2 starts the timer. Next, it runs app.exe, because that is the second command in your routine.
How should I wait for Z:\ to exist?
User avatar
boiler
Posts: 16920
Joined: 21 Dec 2014, 02:44

Re: Timer and menu

09 Apr 2021, 20:28

Is there also some thought that the DriveGet and subsequent lines should occur before the Run line is executed? That doesn’t happen because the code under the Timer label won’t execute for the first time until 500 ms after the timer is set by the SetTimer command. That half second gives more than enough time for the Run line to execute before DriveGet and the lines following it are executed.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Timer and menu

09 Apr 2021, 20:47

boiler wrote:
09 Apr 2021, 20:28
Is there also some thought that the DriveGet and subsequent lines should occur before the Run line is executed? That doesn’t happen because the code under the Timer label won’t execute for the first time until 500 ms after the timer is set by the SetTimer command. That half second gives more than enough time for the Run line to execute before DriveGet and the lines following it are executed.
Even 5000 ms makes no difference. AFAICS Label1 (which mounts the drive) does not finish, and so Run does not execute, until the Timer finishes.
User avatar
mikeyww
Posts: 26879
Joined: 09 Sep 2014, 18:38

Re: Timer and menu

09 Apr 2021, 22:26

No guesswork is required. You can determine this from debugging the script.

A demo is below. Which box do you think will appear first?

Code: Select all

Gosub, Label1
MsgBox, Done!
ExitApp

Label1:
SetTimer, Timer, -500
Return

Timer:
MsgBox, Timer done.
Return
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Timer and menu

09 Apr 2021, 22:54

I would have expected Timer done. to come first; how can I make Run execute after the Timer finishes?
User avatar
mikeyww
Posts: 26879
Joined: 09 Sep 2014, 18:38

Re: Timer and menu

09 Apr 2021, 23:20

Now you get it: setting (starting) the timer happens immediately, but the Timer routine starts only after (when) the timer finishes. Therefore, to Run after the timer finishes, put your Run command into the Timer subroutine.
User avatar
boiler
Posts: 16920
Joined: 21 Dec 2014, 02:44

Re: Timer and menu

09 Apr 2021, 23:40

Ecimeric wrote:
09 Apr 2021, 20:47
Even 5000 ms makes no difference. AFAICS Label1 (which mounts the drive) does not finish, and so Run does not execute, until the Timer finishes.
As it seems you are now realizing, making it longer would make no difference because you actually made it worse. You made it 5 seconds before the drive mounts, and the Run line has already been executed. You might be saying it can't run correctly (or at all) until the drive is mounted, but that Run line has been executed by AHK because of the logic/flow of your script.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Timer and menu

10 Apr 2021, 00:16

mikeyww wrote:
09 Apr 2021, 23:20
Therefore, to Run after the timer finishes, put your Run command into the Timer subroutine.
How can I keep them separate so that I can either run Label1 alone, or Label1 then Label2?
User avatar
mikeyww
Posts: 26879
Joined: 09 Sep 2014, 18:38

Re: Timer and menu

10 Apr 2021, 09:19

They are already in separate routines. If needed, you can add more routines and use Gosub to call them.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Timer and menu

10 Apr 2021, 12:54

Code: Select all

Label2:
    Run app.exe
ExitApp

Label3:
    Gosub, Label1
    Gosub, Label2
return
I tried moving Gosub, Label1 to a separate routine, but Z:\ still does not exist before Gosub, Label2.
User avatar
boiler
Posts: 16920
Joined: 21 Dec 2014, 02:44

Re: Timer and menu  Topic is solved

10 Apr 2021, 13:16

You don’t need to move anything to a new routine. The problem is that when your Label1 routine sets the timer, Z:\ won’t exist until 500 ms later. If you want it to set up Z:\ immediately, then call the Timer subroutine immediately with a gosub as shown below. And you can still set the timer so it occurs every 500 ms after that. Again, the key point is that the first time the routine runs when you set the timer is at the end of the first time period, not immediately.

Code: Select all

Label1:
    gosub, Timer
    SetTimer, Timer, 500
return
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Timer and menu

10 Apr 2021, 16:54

Code: Select all

Label1: ; Mount Z:\ if it is not already mounted
    DriveGet, status, Status, Z:\
    if (status = "Invalid")
       [Mount Z:\]
    Gosub, Timer
    SetTimer, Timer, 500
return

Timer: ; Wait for Z:\ to exist
    DriveGet, status, Status, Z:\
    if (status = "Invalid")
        return
    SetTimer, Timer, Off
return

Label2: ; Mount Z:\ if it is not already mounted, then app.exe
    Gosub, Label1
    Run app.exe
I cannot seem to get Z:\ to exist before continuing with Label2.
User avatar
boiler
Posts: 16920
Joined: 21 Dec 2014, 02:44

Re: Timer and menu

10 Apr 2021, 17:02

I don’t know what goes into mounting the Z drive, but perhaps whatever you do to initiate that takes time to complete so even if you have it doing that first, it’s not complete in time. If you have Gosub, Label1 as the first line of the subroutine in Label2, then it’s definitely executing the AHK lines of code in Label1 first. It just may be an issue with the timing of the things that are kicked off by those lines.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Timer and menu

10 Apr 2021, 17:29

Code: Select all

    Gosub, Label1
    while !FileExist("Z:\")
        Sleep, 500
I figured it out ;)
User avatar
boiler
Posts: 16920
Joined: 21 Dec 2014, 02:44

Re: Timer and menu

10 Apr 2021, 17:32

Nice approach.
User avatar
mikeyww
Posts: 26879
Joined: 09 Sep 2014, 18:38

Re: Timer and menu

10 Apr 2021, 17:36

Code: Select all

If !FileExist(drive := "Z:\")
 ; Mount
While !FileExist(drive)
 Sleep, 500
Run app.exe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Nerafius and 74 guests