Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Multiple timers each waiting for a window to open or close


  • Please log in to reply
3 replies to this topic
echristopherson
  • Members
  • 14 posts
  • Last active: Oct 30 2010 04:00 PM
  • Joined: 30 Mar 2010
I'm trying to write a script that will wait for two specific windows to appear, and when one does, move it to a specified location. While each window remains open, I don't want the script to move it; but once it closes, I want the script to again start waiting for it to open.

In order to wait for both windows simultaneously, I use a timer for each one. I think I don't understand timers very well, though; the script doesn't behave as I think it should, in either version that I've created.

Here is the first version:

#SingleInstance Force
#Persistent
;Critical

#NoEnv                      ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input              ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


; Timer to automatically position MyDocuments window where I like it; - means it will only fire once
SetTimer waitForMyDocumentsToOpen, -1000

; Timer to automatically position real MyComputer window where I like it; - means it will only fire once
SetTimer waitForMyComputerToOpen, -1000



; Wait for My Documents window to open; reposition it where I like it
waitForMyDocumentsToOpen:
    Loop
    {
        IfWinNotExist My Documents
        {
            WinWait My Documents
            MsgBox My Documents opened.
            WinMove 304, 433
        }
        WinWaitClose My Documents
        MsgBox My Documents closed.
    }
Return



; Wait for My Computer window to open; reposition it where I like it
waitForMyComputerToOpen:
    Loop
    {
        IfWinNotExist My Computer
        {
            WinWait My Computer
            MsgBox My Computer opened.
            WinMove 304, 433
        }
        WinWaitClose My Computer
        MsgBox My Computer closed.
    }
Return

This version does correctly detect when the window is opened and closed, but only in the case of whichever timer I set second in the script. It simply ignores the other one.

The second version:
#SingleInstance Force
#Persistent
;Critical

#NoEnv                      ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input              ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


; Timer to automatically position MyDocuments window where I like it; - means it will only fire once
SetTimer waitForMyDocumentsToOpen, -1000

; Timer to automatically position real MyComputer window where I like it; - means it will only fire once
SetTimer waitForMyComputerToOpen, -1000



; Wait for My Documents window to open; reposition it where I like it
waitForMyDocumentsToOpen:
	WinWait My Documents
	MsgBox My Documents opened.
	WinMove 304, 433
	; Watch for window to close; when it does, restart this timer
	SetTimer waitForMyDocumentsToClose, -1000
Return

; Reenable My Documents timer when MyDocuments window is closed
waitForMyDocumentsToClose:
    WinWaitClose My Documents
    MsgBox My Documents closed.
    ; Reenable open timer
    SetTimer waitForMyDocumentsToOpen, -1000
Return



; Wait for My Computer window to open; reposition it where I like it
waitForMyComputerToOpen:
	WinWait My Computer
	MsgBox My Computer opened.
	WinMove 304, 433
	; Watch for window to close; when it does, restart this timer
	SetTimer waitForMyComputerToClose, -1000
Return

; Reenable My Computer timer when MyComputer window is closed
waitForMyComputerToClose:
    WinWaitClose My Computer
    MsgBox My Computer closed.
    ; Reenable open timer
    SetTimer waitForMyComputerToOpen, -1000
Return

This one behaves so erraticly I can't even describe its behavior. It sometimes detects opening and closing of both windows, but not consistently. It also seems to sometimes ignore one window until I either open or close the second, at which point it pops up MsgBoxes for both of them.

When I break these timers into two separate scripts, everything works fine; but I'd rather combine things into one, if possible. Can anyone tell me what I'm doing wrong?

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
I didn't check your code, but you may wanna try WinTrigger.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


echristopherson
  • Members
  • 14 posts
  • Last active: Oct 30 2010 04:00 PM
  • Joined: 30 Mar 2010
Thanks! That works great.

I still don't understand why mine didn't work though :?

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
Probably because WinWait[Close] was causing your script to get stuck since AHK does not support multiple threads.
So, using SetTimer was useless.
WinTrigger checks every X miliseconds if the window is active or exists, it does not hang waiting for the window.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.