OnMessage threads clarification needed Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

OnMessage threads clarification needed

07 Mar 2019, 23:05

Consider the following:

Code: Select all

OnMessage(0x1, "Function1")
OnMessage(0x2, "Function1")	
What would happen if message 0x2 arrived during 0x1's function1?
https://autohotkey.com/docs/commands/OnMessage.htm wrote: If a message arrives while its function is still running due to a previous arrival of the same message...such and such
Yes, but what if its function is still running due to a previous arrival of a different message? Are the rules the same?

Thanks!
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: OnMessage threads clarification needed

08 Mar 2019, 03:54

The wording "... arrival of the same message ..." is used with good cause. Why don't you test it?

Code: Select all

#NoEnv
Gui, Show, x10 y10 w400 h400, OnMessage()
OnMessage(0x0201, "MouseMessage") ; WM_LBUTTONDOWN
OnMessage(0x0204, "MouseMessage") ; WM_RBUTTONDOWN
Return
GuiClose:
ExitApp
MouseMessage(W, L, M, H) {
   MsgBox, % (M = 0x0201) ? "WM_LBUTTONDOWN"
           : (M = 0x0204) ? "WM_RBUTTONDOWN"
           : "Other message: " . Format("0x{04X}", M)
   Return 0
}
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: OnMessage threads clarification needed

08 Mar 2019, 07:14

That msgbox is glorious. Cant be cleaner than that. Off topic sorry but Nice way to EXPLAIN the code with your intuitive layout.. ill try using this kind of indention
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Re: OnMessage threads clarification needed

08 Mar 2019, 20:12

Thanks just me.

I'd prefer to use the workaround of increasing the message check interval, as it seems less convoluted than posting a second message to the script. But it doesn't seem to work: setting Critical 1000 doesn't block the second mouse click from arriving if clicked within a second.

Also what's the deal with the 2 thread limit - only 2 msgboxes can be waiting simultaneously. Is this some kind of hard coded thread limit in ahk?

edit: the Critical problem appears to be related to msgboxes (and also Sleep) since it doesn't occur if doing some other code like this

Code: Select all

#NoEnv
Gui, Show, Center w400 h400, OnMessage()
OnMessage(0x0201, "MouseMessage") ; WM_LBUTTONDOWN
OnMessage(0x0204, "MouseMessage") ; WM_RBUTTONDOWN
Return


MouseMessage(W, L, M, H) {
	
	Critical 3000

	If (M = 0x0201){
		
		MsgBox Starting WM_LBUTTONDOWN
		
		;Sleep 10000			;doesn't work - WM_RBUTTONDOWN will interrupt immediately
		;MsgBox WM_LBUTTONDOWN	;doesn't work - WM_RBUTTONDOWN will interrupt immediately
		
		loop 100000000000		;works - WM_RBUTTONDOWN will buffer for up to 3000ms (but could be
			var++				;much less if ahk's message check interval happened to finish recently!)
		
		MsgBox Finished WM_LBUTTONDOWN
	}

	If (M = 0x204)
		MsgBox WM_RBUTTONDOWN

	Return 0
	
}


GuiClose:
ExitApp

edit: ah
https://autohotkey.com/docs/commands/Critical.htm#Behave wrote:A critical thread becomes interruptible when a MsgBox or other dialog is displayed
So we cannot actually use msgboxes to test this. But it just so happens the result is the same when replacing msgbox with loop var++, and even when making the function Critical (without second parameter).

But there is still the unsolved mystery of why Sleep 10000 allows immediate interrupts, but loop var++ doesn't. And the mystery of the 2 msgbox maximum.
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: OnMessage threads clarification needed  Topic is solved

08 Mar 2019, 23:13

Critical wrote:However, commands that wait such as Sleep and WinWait will check messages regardless of this setting (a workaround is DllCall("Sleep", UInt, 500)).

Source: Message Check Interval


And the mystery of the 2 msgbox maximum.
Two message numbers -> two threads -> two message boxes.
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Re: OnMessage threads clarification needed

08 Jan 2020, 10:01

Just to clarify - will Critical 3000 only be in effect for the thread in which it's declared, or until a Critical Off is encountered?

The documentation isn't entirely clear, it says "Note: Increasing the message-check interval too much may reduce the responsiveness of various events such as GUI window repainting" which seems to imply that it's not thread-specific and that we'd have to manually set Critical Off (but does that actually reset it back to its default (16?)).

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 242 guests