How do I keep this Settimer excuting it's code ??

Get help with using AutoHotkey and its commands and hotkeys
WalkerOfTheDay
Posts: 360
Joined: 24 Mar 2016, 03:01

How do I keep this Settimer excuting it's code ??

14 Jan 2019, 05:23

Hi guys,

I'm having a little with the SetTimer function.

Basically what I'm trying to achieve, is have this piece of code check if a certain
window pops up, and the act upon it.

I have this piece of code in the autoexecute section. This works until I press a hotkey, then it
just pauses until I reload the script.

That probably makes sense, because the documentation is saying:
Although timers may give the illusion that the script is performing more than one task simultaneously, this is not the case. Instead, timed subroutines are treated just like other threads: they can interrupt or be interrupted by another thread, such as a hotkey subroutine. See Threads for details.

So my question is, is there a(nother) way to keep these timers doing their checks while the code under the hotkey is running ?

I now I could run a second script with the SetTimer functions that I'd like to avoid that if possible.

I hope this makes sense :crazy:

relevant part:

Code: Select all

SetTimer, CatchErrors, 50
SetTimer, Afdrukformaat, 50
return

CatchErrors:
imagesearch, tsbX, tsbY, 0,0, A_ScreenWidth, A_ScreenHeight, *30 images\taal_sjabloon.png 
If ErrorLevel = 0
{
	MsgBox, Found !
	Send, {Enter}
}
else
{
	Tooltip, NOT found !
	Sleep, 2000
	ToolTip
}
return
Entire script

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTitleMatchMode, 2
DetectHiddenWindows, On

Progress,2: CWFF0000 CTFFFFFF W200 zh0 fs12 B1 x1 y800, Starten: ctrl-7 `nReload: ctrl-2 `nAfsluiten: ctrl-0

SetTimer, CatchErrors, 50
SetTimer, Afdrukformaat, 50
return

CatchErrors:
imagesearch, tsbX, tsbY, 0,0, A_ScreenWidth, A_ScreenHeight, *30 images\taal_sjabloon.png 
If ErrorLevel = 0
{
	MsgBox, Found !
	Send, {Enter}
}
else
{
	Tooltip, NOT found !
	Sleep, 2000
	ToolTip
}
return

Afdrukformaat:
imagesearch, adfX, adfY, 0,0, A_ScreenWidth, A_ScreenHeight, *30 images\afdrukformaat.png 
if ErrorLevel = 0
{
	Searchbutton("afdrukformaat_ja.png", "afdrukformaat (ja) knop")
	WinWait, TSI Prod, Afdrukinstellingen, 1
	Searchbutton("afdrukinstellingen_ok.png", "afdrukinstellingen (OK) knop")	
	Searchbutton("alle_paginas_afgedrukt.png", "alle pagina's afgedrukt Venster")	
	Send, {Enter}
	
}
return


^0::
MsgBox,,,Emergency keys pressed`, exiting, 2
ExitApp

^2::
Reload
return

^7::
Loop
{

	WinActivate, TSI Prod 
	sleep, 1000
	WinMaximize, TSI Prod
	WinWaitActive, TSI Prod 

	InputBox, vPickNR, Pickid, Voer het picknummer in,, 200, 150
	if ErrorLevel
	{
		MsgBox, CANCEL gedrukt. Opnieuw starten met CTRL-7
		return
	}
		
	InputBox, vColli, Colli, Voer het aantal colli in,, 200, 150,,,,,1
	if ErrorLevel
	{
		MsgBox, CANCEL gedrukt. Opnieuw starten met CTRL-7
		return
	}
	
	IfWinActive,, Picklijst
	{
		Send, !na ; close all windows if picklistscreen is already active
		Searchbutton("hoofdmenu.png", "Hoofdmenu")
	}
	Send {F11}
	WinWait,, Picklijst, 1

	MaxWindow("images\maximize.png", "images\restore_down.png", "Picklijst")	
	MaxWindow(Filename, Filename2, WindowToWaitFor)
	{
		Loop
		{
			; search if the window is maximized, if not maximize it
			imagesearch, varX, varY, 0,0, A_ScreenWidth, A_ScreenHeight, *30 %Filename% 
			if ErrorLevel = 0
			{
				Click, %varX%, %varY%
				Break
			}
			else
			{
				; check if the window is already maximized
				imagesearch, varX, varY, 0,0, A_ScreenWidth, A_ScreenHeight, *30 %FileName2% 
				if ErrorLevel = 0
				{
					Sleep, 1200
					Break
				}
				else
				{
					WinWait,, %WindowToWaitFor%
					continue
				}
					
			}
		}
		
	}
	Send, %vPickNR%{Enter}
	sleep, 1500

	Searchbutton("allepicken.png", "Alle picken knop")

	SearchButton(buttonfile, buttondescription)
	{
		Loop
		{
			Sleep, 500
			imagesearch, buttonX, buttonY, 0,0, A_ScreenWidth, A_ScreenHeight, *30 images\%buttonfile%
			if ErrorLevel = 0
			{
				Click, %buttonX%, %buttonY%
				break
			}
			else
			{
				if A_Index = 1
					Progress, zh0 fs18 B1, Wacht op %buttondescription%
				continue
			}
		}
		Progress, Off ; clear progress text
	}
	Searchbutton("actualiseren.png", "Actualiseren knop")
	Searchbutton("creeren.png", "Crëeren knop")
	Searchbutton("handmatige_factuur.png", "handmatige factuur optie")
	Searchbutton("uitgaande_factuur.png", "uitgaande factuur scherm")
	MaxWindow("images\maximize.png", "images\restore_down.png", "Uitgaande factuur")	
	Searchbutton("colli_selectie.png", "colli veld")
	Send, %vColli%
	Searchbutton("toevoegen.png", "Toevoegen knop")
	Searchbutton("uitgaande_factuur_ja.png", "uitgaande factuur (ja) knop")
	;Searchbutton("pallet_selectie.png", "pallet veld") ;;;;;;;;;;;;;
	WinWait, TSI Prod, Systeemmelding
	Send, {enter}
	WinWaitNotActive, TSI Prod, Uitgaande factuur
	;;;;return  ;; return van ^7

}
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 07:01

One thing is obvious: One timer thread can take more then 2000 ms to finish, but should be started every 50 ms.
Try:

Code: Select all

SetTimer, CatchErrors, -0
SetTimer, Afdrukformaat, 50
return

CatchErrors:
imagesearch, tsbX, tsbY, 0,0, A_ScreenWidth, A_ScreenHeight, *30 images\taal_sjabloon.png 
If ErrorLevel = 0
{
	MsgBox, Found !
	Send, {Enter}
}
else
{
	Tooltip, NOT found !
	Sleep, 2000
	ToolTip
}
sleep, 50
SetTimer, CatchErrors, -0
return
If you still have stall after hotkey use SetTimer, CatchErrors, -0 before return!
WalkerOfTheDay
Posts: 360
Joined: 24 Mar 2016, 03:01

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 07:25

Thanks for you suggestion rommmmcek,

But unfortunatly it doesn't work. As soon as I press the ctrl-7 hotkey, the code in the settimer is halted.
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 07:44

That's normal, so at the end of that hotkey before return use SetTimer, CatchErrors, -0 to start timer again!
WalkerOfTheDay
Posts: 360
Joined: 24 Mar 2016, 03:01

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 07:53

rommmcek wrote:
14 Jan 2019, 07:44
That's normal, so at the end of that hotkey before return use SetTimer, CatchErrors, -0 to start timer again!
Thanks, but that's not what I'm looking for. I want the timers to run while the script under the hotkey is running,
not restart the timer once it's finished.
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 08:05

This is not easy to achieve in one thread Ahk v.1
You can tray to run timer several times form the hotkey.
If this doesn't give you desired results, then you have to run the timer routine form a separate script.
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 08:17

One more idea. Try:

Code: Select all

SetTimer, CatchErrors, 500
return

^F7::SetTimer, TimerF7, -0

CatchErrors:
a++
ToolTip, % a, 0, 0
return

TimerF7:
b++
ToolTip, % b, 100, 0, 2
sleep, 2000
ToolTip,,,, 2
return
Edit: True working example!
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 08:36

This works too:

Code: Select all

SetTimer, CatchErrors, 500
return

CatchErrors:
a++
ToolTip, % a, 0, 0
return

^F7::
b++
ToolTip, % b, 100, 0, 2
sleep, 2000
ToolTip,,,, 2
return
So I guess you have to debag your hotkey routine! Ensure there are enough Sleep commands every while in the routine to ensure running the timer!
WalkerOfTheDay
Posts: 360
Joined: 24 Mar 2016, 03:01

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 09:38

rommmcek wrote:
14 Jan 2019, 08:36
This works too:

Code: Select all

SetTimer, CatchErrors, 500
return

CatchErrors:
a++
ToolTip, % a, 0, 0
return

^F7::
b++
ToolTip, % b, 100, 0, 2
sleep, 2000
ToolTip,,,, 2
return
So I guess you have to debag your hotkey routine! Ensure there are enough Sleep commands every while in the routine to ensure running the timer!
Good to know this works !

I think it isn't working for me because when I press ^7 it runs an 'infinite' loop until I press cancel.
So it never reaches the return.
Hellbent
Posts: 400
Joined: 23 Sep 2017, 13:34

Re: How do I keep this Settimer excuting it's code ??

14 Jan 2019, 14:52

if you run this script a few times you will see that timer1 sometimes stops running and other times it won't.
It looks like if the timer is in the middle of a sleep when the hotkey is pressed it will stall, but timer2 never stalls because there is no sleep.

Code: Select all

#SingleInstance,Force
can_do_Step_1:=1
can_Do_step_2:=1

SetTimer, Timer1, 50
SetTimer, Timer2, 50


return
*Esc::
	ExitApp


Timer1:
	sleep,100
	;~ if(can_do_Step_1 = 1 ){
		;~ can_do_Step_1:=0
		;~ stime1:=A_TickCount
		;~ tooltip, in 1 `na = %a% `nb = %b% `nc = %c%
	;~ }
	;~ else if(A_Tickcount-stime1>5000){
		;~ can_do_Step_1:=1
		a++
	;~ }
	tooltip, in 1 `na = %a% `nb = %b% `nc = %c%
	return

Timer2:
;~ sleep,100
	if(can_Do_step_2=1){
		can_Do_step_2:=0
		stime2:=A_TickCount
		tooltip, in 2 `na = %a% `nb = %b% `nc = %c%
	}
	if(A_Tickcount-stime2>100){
		can_Do_step_2:=1
		b++
	}
	return
	
numpad1::
	Loop	{
		;~ tooltip, in hotkey
		sleep 100
		c++
	}
	return

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], HotKeyIt, kczx3, Tigerlily and 169 guests