Working examples #A and #B. Take into account that while the thread is still active, the user may start a new thread on another Notepad window.
#A using passing trough the pid and #B using the last found window. In #A it may seem like a minor inconvenience to pass the pid. But with many different functions and functions with various inputs, some requiring pid and others don't, it soon becomes messy and difficult to maintain. In #B,
OpenAbout needs to restore the last found window to ensure View Statusbar is triggered correctly.
In the proposed solution #C, pid is a thread global variable. It cannot be altered by another thread. There is no need to pass it trough and it is automatically destroyed at the end of the thread.
Code: Select all
SetTitleMatchMode, 2
#If pid := WinGet("ahk_id" WinActive("- Notepad"), "pid")
#A:: openAboutPID(pid, 5000)
#B:: openAboutLF(5000)
#If
openAboutPID(pid, nseconds)
{
WinMenuSelectItem, - Notepad ahk_pid %pid%, , Help, About
Sleep, %nseconds%
closeAboutPID(pid)
WinMenuSelectItem, - Notepad ahk_pid %pid%, , View, Status Bar
}
closeAboutPID(pid)
{
WinClose, About Notepad ahk_pid %pid%
}
openAboutLF(nseconds)
{
WinMenuSelectItem, , , Help, About
Sleep, %nseconds%
closeAboutLF()
WinMenuSelectItem, , , View, Status Bar
}
closeAboutLF()
{
lf := WinExist()
WinGet, pid, pid
WinClose, About Notepad ahk_pid %pid%
WinExist("ahk_id " lf) ; Restore last found
}
WinGet(title, cmd := "")
{
WinGet, output, %cmd%, %title%
return output
}
How I want it (not working example)
Code: Select all
SetTitleMatchMode, 2
ThreadGlobal threadpid
#If threadpid := WinGet("ahk_id" WinActive("- Notepad"), "pid")
#C:: openAboutTG(5000)
#If
openAboutTG(nseconds)
{
WinMenuSelectItem, - Notepad ahk_pid %threadpid%, , Help, About
Sleep, %nseconds%
closeAboutTG()
WinMenuSelectItem, - Notepad ahk_pid %threadpid%, , View, Status Bar
}
closeAboutTG()
{
WinClose, About Notepad ahk_pid %threadpid%
}
WinGet(title, cmd := "")
{
WinGet, output, %cmd%, %title%
return output
}