Close Window (Passing a variable to timer)

Post your working scripts, libraries and tools for AHK v1.1 and older
jwinfl
Posts: 89
Joined: 16 Aug 2017, 12:37

Close Window (Passing a variable to timer)

14 Dec 2017, 10:02

I looked for a solution without success, figured it out, hopefully this will save someone else a some time.

code starts timer > runs notepad > after 2 sec closes notepad (should work with any window title or ahk_??? window title parameter)

Below is the code (added #Persistent per Wolf_II post)...

Code: Select all

#Persistent
CloseWindow("Untitled - Notepad",2000)
run notepad.exe
return

; the following Function & Timer can be in the main script or in a function library

CloseWindow(wTitle,DelayInMilSecs) ; CLOSES Window after Delay (MilSec)
{
	global wTTC := wTitle
	SetTimer, CloseWindow_Timer, % "-" . DelayInMilSecs
	return
}
CloseWindow_Timer:
	WinClose %wTTC%
	Return
Last edited by jwinfl on 14 Dec 2017, 13:02, edited 4 times in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: SOLVED - Close Window (PASSING A VAIRABLE TO TIMMER)

14 Dec 2017, 10:23

Your script does not work.
1 - First line is not recognized as ahk code. (Likely some mistake during writing the post rather than writing the script.)
2 - Unless you make it #Persistent, the script will terminate before the timer gets called :(
jwinfl
Posts: 89
Joined: 16 Aug 2017, 12:37

Re: SOLVED - Close Window (PASSING A VAIRABLE TO TIMMER)

14 Dec 2017, 13:01

Thanks Wolf_II
Added #Persistent to original post per Wolf_II and figured out how to insert code so it is correct in forum post.
Ran it, works correctly in W10x64.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: SOLVED - Close Window (PASSING A VAIRABLE TO TIMMER)

14 Dec 2017, 13:13

Thank you for the update, and for sharing the function. :)
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Close Window (Passing a variable to timer)

17 Dec 2017, 16:05

Worked for me too (Windows XP).
truekefir
Posts: 17
Joined: 29 Jul 2017, 13:57

Re: Close Window (Passing a variable to timer)

05 Jan 2018, 23:38

Code: Select all

#Persistent
#SingleInstance Force

CloseWindow(hwnd, ExitApp, prm)
{	
	fn := prm.fn
	SetTimer % fn, Delete
	WinClose ahk_id %hwnd%
	If ExitApp
		ExitApp
	return
}

delay:= 7000
ExitAppAfterWinClose := 0
Run, notepad.exe, ,, ID
WinWait ahk_pid %ID%
WinGet, hwnd, ID, A
CloseWindow_fn := Func("CloseWindow").bind(hwnd, ExitAppAfterWinClose, CloseWindow_prm := {})
CloseWindow_prm.fn := CloseWindow_fn
SetTimer, % CloseWindow_fn, -%delay%

sleep 3000

Run, calc.exe, ,, ID
WinWait ahk_pid %ID%
WinGet, hwnd, ID, A
ExitAppAfterWinClose := 1
CloseWindow_fn := Func("CloseWindow").bind(hwnd, ExitAppAfterWinClose, CloseWindow_prm := {})
CloseWindow_prm.fn := CloseWindow_fn
SetTimer, % CloseWindow_fn, -%delay%

return
I would do it like this to ensure it closes the right program, and if you want to use another timer to close another window while 1st timer is running they won't interfere with each other

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 80 guests