The ErrorLevel value of each thread is not independent Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

The ErrorLevel value of each thread is not independent

Post by feiyue » 21 Nov 2021, 14:15

[Moderator's note: Topic moved from Bug Reports.]

The help file says: each thread maintains its own ErrorLevel value,
But I found that the ErrorLevel value of new thread inherits the previous thread instead of starting from 0,
which makes the ErrorLevel value of each thread is not independent. For example:

Code: Select all

F1:: Msgbox, % ErrorLevel:=123
F2:: Msgbox, % ErrorLevel

gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: The ErrorLevel value of each thread is not independent  Topic is solved

Post by gregster » 21 Nov 2021, 14:25

The docs actually say about the original - interrupted - thread (and not about the interrupting one):
https://www.autohotkey.com/docs/misc/ErrorLevel.htm wrote: if the current thread is interrupted by another, when the original thread is resumed it will still have its original value of ErrorLevel, not the ErrorLevel that may have been set by the interrupting thread.
Also,
https://www.autohotkey.com/docs/misc/Threads.htm wrote:When the current thread finishes, the one most recently interrupted will be resumed, and so on, until all the threads finally finish. When resumed, a thread's settings for things such as ErrorLevel and SendMode are automatically restored to what they were just prior to its interruption; in other words, a thread will experience no side-effects from having been interrupted (except for a possible change in the active window).
I understand it this way:

Code: Select all

F1:: 
Msgbox, % ErrorLevel:=123
msgbox % "F1 resumes: " ErrorLevel
Return

F2:: Msgbox, % ErrorLevel := 5
Press F1 ("current thread"), then F2 (F2's thread interrupts F1's thread), then close both initial msgboxes; F1's thread resumes.
The final msgbox of F1 still shows 123, although F2 has set its own Errorlevel to 5 in the meantime. Looks like it's documented, afaics.
Last edited by gregster on 21 Nov 2021, 17:02, edited 2 times in total.
Reason: additions

feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: The ErrorLevel value of each thread is not independent

Post by feiyue » 21 Nov 2021, 22:21

I found a thread independent variable that can be set, which can be used as follows:

Code: Select all

F1:: 
Msgbox, % A_LastError
DllCall("SetLastError", "uint",555555)
Msgbox, % A_LastError
return

F2::
Msgbox, % A_LastError
DllCall("SetLastError", "uint",666666)
Msgbox, % A_LastError
return

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: The ErrorLevel value of each thread is not independent

Post by lexikos » 22 Nov 2021, 02:37

That the thread maintains its own value for ErrorLevel says nothing about what value it will have when the thread comes into existence.

It would be more intuitive for ErrorLevel to be reset to 0 for each new thread, but the behaviour cannot be changed in v1. It was changed in v2-alpha, but then ErrorLevel was completely removed.

Post Reply

Return to “Ask for Help (v1)”