Page 1 of 1

Wait till previous ahk instance (of single script) is finished

Posted: 16 Dec 2016, 14:58
by j-t-r
Hello,
I use USB Drive Letter Manager for Windows (http://www.uwe-sieber.de/usbdlm_e.html) which allows me to run ahk script (lets say usbrun.ahk) whenever is any usb drive successfully mounted. Is there any easy way how to make second instance wait for the first to finish, third instance wait for the second to finish etc. I mean in case when more than one usb drive is successfully mounted in short period of time.
I will appreciate your help with this situation.
BR
j-t-r

Re: Wait till previous ahk instance (of single script) is finished

Posted: 16 Dec 2016, 21:12
by j-t-r
In other words I am looking for something like WaitTillAllPreviouslyStartedInstancesOfThisScriptAreFinished command ;-)
To paste it in the first line...

Re: Wait till previous ahk instance (of single script) is finished

Posted: 16 Dec 2016, 23:58
by Xeo786
:idea:

Code: Select all

runwait, c:\usbrun.ahk

Re: Wait till previous ahk instance (of single script) is finished

Posted: 17 Dec 2016, 04:58
by j-t-r
Thank you for your reply. Unfortunately, this is not it. I just tried. Even if I add say usbrun2.ahk with only one line (runwait, c:\usbrun.ahk) to the USB Drive Letter Manager it will not prevent multiple instances of usbrun.ahk from starting sooner that all previously started ones are finished.

Re: Wait till previous ahk instance (of single script) is finished

Posted: 17 Dec 2016, 05:08
by j-t-r
Ideally, I would need something like #SingleInstance wait -> The word WAIT skips the dialog box and leaves the old instance running. The new one (first from the queue) will start as soon as previously started one is finished.
Since there is nothing like this I hope there is at least some elegant workaround...

Re: Wait till previous ahk instance (of single script) is finished

Posted: 17 Dec 2016, 05:32
by Guest
Perhaps:

a - use another script to run usbrun.ahk, that way you can check in the script that runs usbrun.ahk if there is window (each script has a window) - of so wait a second, check again and so on

b - use something like the code below in your usbrun.ahk - it uses #SingleInstance, off so you can start it as many times as you like but it counts the number of windows, if you add a settimer to check this at regular intervals and only continue of there is only one window (e.g. the currently running script) you can build some sort of queue system. the sleep 1000 below is just to give you time to start a second version of the script manually for testing purposes.

Code: Select all

DetectHiddenWindows, On
#SingleInstance, off

sleep 1000 ; just for testing purposes
WinGet, id, list,,, Program Manager
usbrun:=0
Loop, %id%
{
	this_id := id%A_Index%
	WinGetTitle, this_title, ahk_id %this_id%
	if InStr(this_title,"usbrun.ahk")
		usbrun++
}
MsgBox % usbrun

Re: Wait till previous ahk instance (of single script) is finished  Topic is solved

Posted: 17 Dec 2016, 14:05
by j-t-r
Thank you for reminding me that all ahk scripts have windows ;-) While keeping this in mind I was able to come with this:

Code: Select all

#SingleInstance, off
SetTitleMatchMode,2
DetectHiddenWindows, On
WinGet, this, id, usbrun.ahk - AutoHotkey
loop
{
WinGet, last, idlast, usbrun.ahk - AutoHotkey
if this = %last%
break
}
do whatever you want