SendMessage timeout Topic is solved
SendMessage timeout
Perhaps it could be mentioned that timeout can be 0 or 0xFFFFFFF for an indefinite wait. In C++ the indefinite wait is defined as the maximum unsigned int #define INFINITE 0xFFFFFFFF, but in AHK the unsigned integer gets converted to a signed integer (even though SendMessage timeout parameter should be unsigned), so it gets converted to -1 and timeouts immediately.
Re: SendMessage timeout
You're absolutely right, but would you ever use an infinite timeout with SendMessage? Even a timeout of 4,294,967,295 (0xFFFFFFFF) ms seems to be clearly exaggerated. The default value of 5000 ms is most probably a leftover from 'good old Win 95/98' times.
Re: SendMessage timeout
@just me I opened this thread exactly because I wanted (though not necessarily needed) to use an infinite timeout. I could ask the same question about WinWaitActive (or WinWait, or ...) - why should the default be infinite wait, not 30 seconds or something like that?
The default value 5000ms for SendMessage seems like an arbitrary value from AHK v1 times, because Win32 SendMessage by default has an infinite wait. I would have expected AHK to be the same, but I guess because AHK is single-threaded then this is necessary to prevent the script from freezing? In that case perhaps SendMessageCallback should've been the default instead...
The default value 5000ms for SendMessage seems like an arbitrary value from AHK v1 times, because Win32 SendMessage by default has an infinite wait. I would have expected AHK to be the same, but I guess because AHK is single-threaded then this is necessary to prevent the script from freezing? In that case perhaps SendMessageCallback should've been the default instead...
Re: SendMessage timeout
@Descolada, I never used one of the ...Wait... commands without a timeout in my scripts.
IMO, an infinite wait is useless in this cases too.
And yes, AHK internally calls SendMessageTimeout to prevent scripts from freezing.
And because SendMessage is always used for communications expecting a direct answer, an infinite wait is much more than reasonable. I'd prefer infinite - 10!
IMO, an infinite wait is useless in this cases too.
And yes, AHK internally calls SendMessageTimeout to prevent scripts from freezing.
And because SendMessage is always used for communications expecting a direct answer, an infinite wait is much more than reasonable. I'd prefer infinite - 10!
Re: SendMessage timeout
I agree, I also refrain as much as humanly possible to have infinite waiting in any script.
Mostly because is really tricky to debug. The script simply stops (usually without any indication that something is wrong) because you assumed the waiting condition was going to be met, but it didnt for whatever random reason.
So, trying to find out what happened is usually achieved by opening the hidden ahk window and checking the last lines executed... thats where you see the loop happening.
Projects:
AHK-ToolKit
AHK-ToolKit
Re: SendMessage timeout
Before I add this to the docs, I would like to test it. Do you have any examples?
Re: SendMessage timeout
@Ragnar
Receiver.ahk (run first):
Sender, waits indefinitely:
Sender, times out immediately:
Sender, SendMessage with default timeout (times out in 5 seconds):
Receiver.ahk (run first):
Code: Select all
#Requires AutoHotkey v2.0
; Register a new window message with the custom name "NewAHKScript"
MsgNum := DllCall("RegisterWindowMessage", "Str", "NewAHKScript")
OnMessage(MsgNum, NewScriptCreated)
Persistent()
NewScriptCreated(wParam, lParam, msg, hwnd) {
Loop {
ib := InputBox("Script with hWnd " hwnd " sent message:`n`nwParam: " wParam "`nlParam: " lParam "`n`nReply:", "Message")
if ib.Result = "Cancel"
return 0
else if !IsInteger(IB.Value)
MsgBox "The reply can only be a number", "Error"
else
return IB.Value
}
}
Code: Select all
#Requires AutoHotkey v2.0
DetectHiddenWindows 1
receiverhWnd := WinExist("Receiver.ahk ahk_class AutoHotkey")
MsgNum := DllCall("RegisterWindowMessage", "Str", "NewAHKScript")
reply := SendMessage(MsgNum, 123, -456,, receiverhWnd,,,, 0)
MsgBox "Got reply: " reply
Code: Select all
#Requires AutoHotkey v2.0
DetectHiddenWindows 1
receiverhWnd := WinExist("Receiver.ahk ahk_class AutoHotkey")
MsgNum := DllCall("RegisterWindowMessage", "Str", "NewAHKScript")
reply := SendMessage(MsgNum, 123, -456,, receiverhWnd,,,, 0xFFFFFFFF)
MsgBox "Got reply: " reply
Code: Select all
#Requires AutoHotkey v2.0
DetectHiddenWindows 1
receiverhWnd := WinExist("Receiver.ahk ahk_class AutoHotkey")
MsgNum := DllCall("RegisterWindowMessage", "Str", "NewAHKScript")
reply := SendMessage(MsgNum, 123, -456,, receiverhWnd)
MsgBox "Got reply: " reply
Re: SendMessage timeout Topic is solved
Thanks for the suggestion and the nice examples. I've updated the Timout parameter description.