1.) I want to return a value from the "Timer" function - Is it possible?
2a.) Is the above timer only running once? (I don't need the variabel above StopTimer / StopValue?)
2b.) If not 2a. -Able to stop the timer (SetTimer MyTimer, off) in the timer "MyTimer" - Is it possible?
3.) The system variable A_ThisLabel / SetTimer never contains a value, why?
#SingleInstance Force
Global TimerStatus ; Possible to get a value from SetTimer
TimerValue = 50
First(TimerValue)
ExitApp
Esc::ExitApp
First(StopTimer)
{ TimerStatus = no
fn := Func("MyTimer").Bind(A_ThisFunc, "123-321", StopTimer)
SetTimer % fn, On
Sleep 250 ; Allows to start SetTimer before next instruction
If ( TimerStatus = "yes" )
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, TimerValue(%StopTimer%) - MORE than 25
If ( TimerStatus = "no" )
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, TimerValue(%StopTimer%) - LESS than 25
SetTimer % fn, -100
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, A_ThisLabel .: %A_ThisLabel% `nStopTimer = %StopTimer% `nTimerStatus = %TimerStatus%
}
MyTimer(Func, y, Value)
{ If ( Value > 25 )
{ TimerStatus = yes
; SetTimer % fn, off ; Not possible to stop the SetTimer in the timer object
; Return %TimerStatus%
}
}
0.) A new question .:
How to create / start a timer object faster?
In the test-script it take almost 1/4 sec (on my computer) to activate the SetTimer object.
To handle this I have added an "unnecessary" delay, like this .:
Global TimerStatus ; Possible to get a value from SetTimer
2.) Still unable to exit/stop the SetTimer in the timer object eg. SetTimer % fn, off ; Not possible to stop the SetTimer in the timer object. Outside the Timer object, it's no problem with .:
1) No. SetTimer is a command, not a function, and as such has no return value
The timer function is being called asynchronously, and as such there is no concept of a return value
2) No, because you did not specify a negative value for the timer. In fact, you specified no value at all, you just specified "On", so it just uses the default timing value of 250.
3) A_ThisLabel never holds anything because MyFunc is not a label, it is a function - A_ThisFunc would give you the name of the function
0) SetTimer does not fire the function immediately. If you specify a period of 250, the first fire of the function will be in 250ms
If you want the timer to fire immediately, just call the function when you set the timer.
Otherwise, I have no idea what you are trying to achieve.
If you want to explain it to me, I can probably show you how to achieve it.
Thanks for your time!
My wish is to handle a number of popup messages that can be displayed or not...
I want to use the same program for multiple purposes. The program I want to manage, handles products / article numbers.
As an example ..: a.) Suppose the products I want to search for, should be in the database (existing products).
If the product I search for, is not found (error) - a popup message appear.
This message should be handled (be closed), and some information about that article/article number, should be saved in a logfile.
If the desired article number is found, the item is shown on the window (no problem - no popup message)
b.) Suppose the products I want to search for, should not be in the database (new products).
In this case, I always want to see the popup message "Product missing!".
If the article number is found, the popup message is not appear and the item is shown on the window (error - no popup message)
My idea was .: 1.) Search for an article number.
2.) Wait for the product or pop-up message to appear.
3.) Handle what should happens
But I must know if the popup message is closed by the timer. (There are some different popup messages that may occur).
___________________________________
0.) Is there anything else I missed? I still need Sleep 100 if I start the timer with SetTimer % fn, 1
0)
A timer value of 1 will not fire every 1ms, it will fire every ~15ms (Due to minimum timer granularity)
As I said, SetTimer fires **for the first time** after the specified period. If you want to "fire it immediately", **just call what the timer would call**, like so:
"If the product I search for, is not found (error) - a popup message appear." - What do you mean by this? The AHK script should create the popup, or the popup will appear as a result of what the AHK script does, and it needs to deal with it?
If you can create some mock code that gives me something to work with, I can maybe help further, but as it is, it's all a bit abstract to be able to come up with implementation suggestions
0)
A timer value of 1 will not fire every 1ms, it will fire every ~15ms (Due to minimum timer granularity)
As I said, SetTimer fires **for the first time** after the specified period. If you want to "fire it immediately", **just call what the timer would call**, like so:
"If the product I search for, is not found (error) - a popup message appear." - What do you mean by this?...
(I'll try to explain again). a.) Suppose I want to process, already existing products in the program I want to work with.
eg. When an article number is entered into the search field of the program I want to control, I expect only one answer.
(several different answers can occur). Here I focus on two of the answers from the program I want to control .: a1. - The expected answer .: The article number is found in the program I want to control, as answer product information is showed. a2. - Not expected answer .: When the article number is NOT found in the program I want to control, an answer, a popup message is displayed with the information "The product could not be found! Should a new product be created?" (with the buttons Yes / No). In this case, my answer is No (the popup message from the program I want to control, disappears). After that some information, that the article number is not found, is saved in a log file.
A similar reasoning, as above, can be done if only new products are to be handled, but now I want to create new products instead.
If you can create some mock code that gives me something to work with, I can maybe help further...
Today I have an AHK program that handles some situations (with several thousand lines). But now I want to look over the whole structure, try to build with object, work with more modules (easier to complement functions / find problems and errors, etc.). Right now I'm trying to see if it is possible for me, to handle pop-up messages, using the SetTimer function. Under the "Spoiler" button is a rough structure of one part of the program.
WinID = Fackhandelsvaror ahk_exe Pvxwin32.exe ahk_class #109
; ProdInfo = NewProd ; Only NEW products are expected
ProdInfo = OldProd ; Only existing products are expected
SeekTime = 5 ; Product search - maximum 5 seconds
ReadFile(FileName.csv) ; Read some CSV-files with information
Loop
{ Count Items ; Count the article numbers in the file
ProductInfo ; Read some product information and create an object
}
Loop %Items% ; Treat each item number in one of the CSV-files
{
ArticleSearch(ArticleNo, ProdInfo, WinID, SeekTime)
. . .
}
; - - - - - - - - - - - - - - - - - - - - - - - -
ExitApp
ArticleSearch(ItemNo, ProdInfo, WinID, SeekTime)
{
; Input the ItemNo to the SearchField
ControlFocus Edit1, %WinID%
ControlSetText Edit1,, %WinID% ; Clear the field
ControlSend Edit1, %ItemNo%, %WinID% ; Enter the desired Article number in the field
; Finish the input with "Enter"
ControlSetText Edit1, %ProdID%, %WinID%
ControlFocus Edit1, %WinID%
ControlSend Edit1, {Enter}, %WinID%
; -----------------------
; Check if the value in the search field is still correct
ControlGetText TxtEdit1, Edit1, %WinID%
Loop
{ If TxtEdit1 <> %ItemNo%
. . .
Break
}
; The input value is Correct
; -----------------------
; Wait for the result - Is the "ItemNo" found or not
ErrWinID = Meddelande ahk_class #32770 ahk_exe PvxWin32.exe
WinText1 = Posten Saknas! Skapa en Ny? ; The text in a popup message
TimerArtMissed := Func("PopupHandle").Bind(ProdID, ProdInfo, ErrWinID, WinText1)
SetTimer % TimerArtMissed, 1
PopupHandle(ProdID, ProdInfo, ErrWinID, WinText1)
Loop
{ Has the pop-up message "Product is missed!" been displayed / managed?
Has another pop-up message been displayed / managed?
Is the ItemNo found?
Is the search time over?
}
}
PopupHandle(ProdID, ProdInfo, ErrWinID, WinText1)
{ If WinExist(ErrWinID, WinText1) ; If the popup message exist
{ If ProdInfo = OldProd ; Not expected result
{
Write to the logfile .: ProdID, ProdInfo, Date, Time
; Close the popupmessage
WinButt = Button2 ; => Nej
ControlSend %WinButt%, {Enter}, %ErrWinID%
WinWaitClose %ErrWinID%, %WinText1%, 3
if ErrorLevel
...
Return
}
}
}
The events I'm now trying to control are .: Has the pop-up message "Product is missed!" been displayed / managed?
Has another pop-up message been displayed / managed?
Is the ItemNo found?
Is the search time over?
Now I can close the popup messages, using the SetTimer function (smoothly). But how to know that the message has been closed with the SetTimer and continue with the AHK program run? Can't get any results from "SetTimer" (and want to avoid Global variables)
Other things that do not feel perfect are eg. to write information to certain cells (eg the entry of article numbers to a field under the "Spoiler button" above), and a good object structure with field names etc.
Thank you everyone! (I try to notice all the good answers, but I only manage to notice one answer - unfortunately!)
Summarizes the above with the following script