SendMessage timeout Topic is solved

Share your ideas as to how the documentation can be improved.
Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

SendMessage timeout

08 Jan 2024, 03:45

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.
just me
Posts: 9466
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: SendMessage timeout

09 Jan 2024, 04:32

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.
Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: SendMessage timeout

09 Jan 2024, 11:07

@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...
just me
Posts: 9466
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: SendMessage timeout

09 Jan 2024, 12:28

@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! :)
User avatar
RaptorX
Posts: 386
Joined: 06 Dec 2014, 14:27
Contact:

Re: SendMessage timeout

09 Jan 2024, 13:03

just me wrote:
09 Jan 2024, 12:28
@Descolada, I never used one of the ...Wait... commands without a timeout in my scripts.
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
User avatar
Ragnar
Posts: 614
Joined: 30 Sep 2013, 15:25

Re: SendMessage timeout

06 Mar 2024, 03:42

Before I add this to the docs, I would like to test it. Do you have any examples?
Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: SendMessage timeout

06 Mar 2024, 05:40

@Ragnar

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
    }
}
Sender, waits indefinitely:

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
Sender, times out immediately:

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
Sender, SendMessage with default timeout (times out in 5 seconds):

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
User avatar
Ragnar
Posts: 614
Joined: 30 Sep 2013, 15:25

Re: SendMessage timeout  Topic is solved

07 Mar 2024, 08:36

Thanks for the suggestion and the nice examples. I've updated the Timout parameter description.

Return to “Suggestions on Documentation Improvements”

Who is online

Users browsing this forum: No registered users and 45 guests