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

Get help with using AutoHotkey and its commands and hotkeys
j-t-r
Posts: 16
Joined: 08 Jun 2015, 03:05

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

16 Dec 2016, 14:58

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
j-t-r
Posts: 16
Joined: 08 Jun 2015, 03:05

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

16 Dec 2016, 21:12

In other words I am looking for something like WaitTillAllPreviouslyStartedInstancesOfThisScriptAreFinished command ;-)
To paste it in the first line...
User avatar
Xeo786
Posts: 193
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

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

16 Dec 2016, 23:58

:idea:

Code: Select all

runwait, c:\usbrun.ahk
"When there is no gravity, there is absolute vacuum" -Game changer theory
j-t-r
Posts: 16
Joined: 08 Jun 2015, 03:05

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

17 Dec 2016, 04:58

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.
j-t-r
Posts: 16
Joined: 08 Jun 2015, 03:05

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

17 Dec 2016, 05:08

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...
Guest

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

17 Dec 2016, 05:32

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
j-t-r
Posts: 16
Joined: 08 Jun 2015, 03:05

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

17 Dec 2016, 14:05

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

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Tom Harding and 257 guests