Wait(Millisekunden)

Veröffentliche deine funktionierenden Skripte und Funktionen

Moderator: jNizM

User avatar
Holle
Posts: 187
Joined: 01 Oct 2013, 23:19

Wait(Millisekunden)

07 Oct 2013, 11:59

Dieses Script ist eine Alternative zu Sleep.

Statt "Sleep, 1000" kann man dieses Script mit "Wait(1000)" benutzen.

Der Unterschied zwischen Sleep und Wait ist folgender:
Sleep unterbricht das Script für x Millisekunden, und führt danach das Script fort.
Wait hingegen erzeugt nach einer x Millisekunden die globale Variable "wait_done", lässt das Script dabei aber weiter laufen.

Hier ist das eigentliche Script:
wait.ahk

Code: Select all

wait(ms) {
	global wait_done
	wait_done := 0
	SetTimer, wait, %ms%
	return
}

wait:
{
	SetTimer, wait, off
	wait_done := 1	
	return
}
So wird Wait() verwendet / to use Wait() :

Code: Select all

; In functions it is needed to set the variable wait_done global.
; global wait_done

Wait(Millisekunden)
   while(!wait_done)
   {
      ; put the code at this place, what continue runs while wait() is active
      ; example:
      ; while (GetKeyState("Space" , "P"))
            ; MsgBox Space was pressed!!!
   }
; at this point the script continue after wait() is done
Hier ein Beispiel an dem man den Unterschied zwischen Sleep und Wait erkennt. Ziel ist es das Script für 5 Sekunden zu unterbrechen, in dieser Zeit soll jedoch auf ein Ereignis gewartet werden (in Beispiel ist das Ereignis die Prüfung ob "Space" gedrückt wurde)

Code: Select all

#Persistent
#SingleInstance

wartezeit := 5000

;~ gosub sleep_test
gosub wait_test

return
#include wait.ahk

sleep_test:
{
	TrayTip,, Sleep = %wartezeit% ms
	Sleep, %wartezeit%
	while (GetKeyState("Space" , "P"))
			MsgBox Space wurde gedrückt!!! 
	MsgBox, Sleep ist fertig 
ExitApp
}

wait_test:
{
	TrayTip,, Wait = %wartezeit% ms
	Wait(wartezeit)
	while(!wait_done)
	{
		while (GetKeyState("Space" , "P"))
    			MsgBox Space wurde gedrückt!!! 
	}
	MsgBox, Wait ist fertig
ExitApp
}

Edit: Spoiler entfernt, da dieser sich (in meinem Browser) nicht öffnen ließ
Last edited by Holle on 21 Nov 2013, 14:54, edited 3 times in total.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Wait(Millisekunden)

08 Oct 2013, 04:44

what are the advantages of this wait() function vs. a simple sleep command (haven't tested ;) )?
User avatar
Holle
Posts: 187
Joined: 01 Oct 2013, 23:19

Re: Wait(Millisekunden)

08 Oct 2013, 08:46

The difference is the following:
Sleep paused the script for x milliseconds. After that the script continue at that point.
Wait() creates a global variable "wait_done", but the script continue while waiting.
Try the test-script in the post before. If you use Sleep, the GetKeyState does´nt work while sleep is active. When you use Wait(), you can check for pressed Space-Key while wait() is active.
User avatar
Seidenweber
Posts: 17
Joined: 30 Sep 2013, 11:40

Re: Wait(Millisekunden)

13 Oct 2013, 14:24

..seems to be the same like this somehow:

Code: Select all

    x := A_TickCount + 5000
    While (A_TickCount < x) {
        ToolTip % x - A_TickCount
        Sleep, 50
    }
    ToolTip
achso .. hier ist ja Doitch. Scheint irgendwie dasselbe zu sein, wie dieser Code, oder?
Questions and answers are related to AHK 1.1.13.01 (x64) & Win 7 pro x64
User avatar
Holle
Posts: 187
Joined: 01 Oct 2013, 23:19

Re: Wait(Millisekunden)

21 Nov 2013, 15:03

Naja, das ist fast das gleiche.
In deinem Beispiel wäre eine Verzögerung von 50ms.
Außerdem bremst das Sleep das Script aus (ist natürlich nur bei komplexen/zeitkritischen Anwendungen relevant).

Aber mir ist durch dein Beitrag ein Fehler in meinem Script aufgefallen. Da hatte ich noch nicht alle Variabeln von "wait_finish" in "wait_done" umbenannt, was ich hiermit nachgeholt habe ;-)

Return to “Skripte und Funktionen”

Who is online

Users browsing this forum: No registered users and 2 guests