Page 1 of 1
OnMessage() Cause temporary variables to empty
Posted: 29 Jan 2019, 18:37
by feiyue
When I used OnMessage ()+SetTimer, I found that the variables of the subroutine inside the function were confusing.
Look at the following example. Why is the value of variable āiā empty?
Code: Select all
Gui, Show, w500 h300
OnMessage(0x200, "WM_MOUSEMOVE")
return
WM_MOUSEMOVE() {
SetTimer, Test, -100
return
Test:
i=1
Loop 10 {
Sleep, 100
j=1
} Until (i!=j)
MsgBox, %i% - %j%
return
}
Re: OnMessage() Cause temporary variables to empty
Posted: 29 Jan 2019, 20:04
by kczx3
Labels inside functions is just a bad idea anyways...
Re: OnMessage() Cause temporary variables to empty Topic is solved
Posted: 30 Jan 2019, 01:16
by Helgef
When a function returns, its non-static variables are made blank.
docs wrote:subroutines should use only static and global variables (not locals) if their function is ever called normally. This is because a subroutine thread that interrupts a function-call thread (or vice versa) would be able to change the values of local variables seen by the interrupted thread. Furthermore, any time a function returns to its caller, all of its local variables are made blank to free their memory.
Re: OnMessage() Cause temporary variables to empty
Posted: 30 Jan 2019, 10:58
by User
Yha, it seems that "labels" inside functions are not recommended in some situations!
You can use the below alternative:
Code: Select all
Gui, Show, w500 h300
OnMessage(0x200, Func("WM_MOUSEMOVE").Bind("OnMessage_Call"))
return
guiclose: ;________________
exitapp
WM_MOUSEMOVE(Options := "") { ;_______________________________
if (Options = "OnMessage_Call")
{
;msgbox, OnMessage_Call Test
SetTimer, % A_ThisFunc, -100
return
}
i=1
Loop 10 {
Sleep, 100
j=1
} Until (i!=j)
MsgBox, %i% - %j%
}
Re: OnMessage() Cause temporary variables to empty
Posted: 07 Mar 2019, 03:58
by just me
Helgef wrote: ā30 Jan 2019, 01:16
When a function returns, its non-static variables are made blank.
So
Code: Select all
WM_MOUSEMOVE() {
Static c, i, j
SetTimer, Test, -100
Return
Test:
i := 1
Loop 10 {
Sleep, 100
c := A_Index
j := 1
} Until (i != j)
MsgBox, %i% - %j% - %c%
Return
- It isn't a bug!
Re: OnMessage() Cause temporary variables to empty
Posted: 08 Apr 2019, 15:45
by feiyue
Yes, I didn't realize before that subroutines in functions would be affected by this.