Problem with ErrorLevel

Get help with using AutoHotkey and its commands and hotkeys
User avatar
cybermcm
Posts: 4
Joined: 09 Mar 2015, 11:32

Problem with ErrorLevel

09 Mar 2015, 11:54

Hi,

Since version 1.1.20.00 Errorlevel behaves different. Example code:

Code: Select all

^!2:: ; loads Add/Remove programs
SetTitleMatchMode, 2
SetTitleMatchMode, Slow
IfWinExist, Add or Remove
{
	Sleep, 500
	WinActivate, Add or Remove
}
else
{
	Run, appwiz.cpl
}

if errorlevel <> 0
{
	msgbox, There seems to be an error :-( %ErrorLevel%
}
return
This script worked until now. New behavior: appwiz.cpl gets started but the error message also appears because ErrorLevel seems to be empty (it should be zero).
Is this a problem with my script or a bug?

greetings,
CyberMCM
Coco-guest

Re: Problem with ErrorLevel

09 Mar 2015, 13:57

Can you try: Run, appwiz.cpl,, UseErrorLevel
Docs wrote:Run: Does not set ErrorLevel unless UseErrorLevel (above) is in effect, in which case ErrorLevel is set to the word ERROR upon failure or 0 upon success.

Source: Run -> ErrorLevel
User avatar
cybermcm
Posts: 4
Joined: 09 Mar 2015, 11:32

Re: Problem with ErrorLevel

09 Mar 2015, 16:00

Coco-guest wrote:Can you try: Run, appwiz.cpl,, UseErrorLevel
Thanks for pointing this out. It worked until the very last version (1.1.20.00)
lexikos
Posts: 7057
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Problem with ErrorLevel

09 Mar 2015, 19:15

It relates to this change:
Fixed thread interrupts to save and restore ErrorLevel more fully, which fixes:
  • Strings longer than 127 chars being truncated.
  • Objects being discarded.
  • Interruption by a timer sometimes causing ErrorLevel to be given hexadecimal formatting.
This had an unintended impact on backward-compatibility, so will be reverted soon and replaced with an alternative fix for the timer issue.

The problem is that previously, ErrorLevel was not reset when a new thread starts. Instead, ErrorLevel retains the value from the previous thread. (Since releasing v1.1.20.00 I've found comments by Chris indicating that this behaviour was intended, primarily for performance reasons.) When the new thread finishes, the value is restored to the value it had when the previous thread was interrupted. Currently in v1.1.20.00, ErrorLevel is cleared (made empty) when the new thread starts. This was unintended.

Note that a consequence of the old behaviour is that your script might not work correctly if some other thread was running and had a non-zero ErrorLevel. In addition to UseErrorLevel, you should set ErrorLevel := 0 in the other IF branch or at the start of the hotkey.

As it is (without UseErrorLevel), there's no point in checking ErrorLevel because it's not set by any of the commands you've used.
User avatar
cybermcm
Posts: 4
Joined: 09 Mar 2015, 11:32

Re: Problem with ErrorLevel

10 Mar 2015, 02:52

Hi lexikos,

Thank you for your detailed explanation. I was confused because my script worked for years and now there was this new behavior. I'll use "UseErrorLevel" for my purpose.

Greetings,
CyberMCM

Return to “Ask For Help”

Who is online

Users browsing this forum: Epialis, hasantr, mikeyww, songdg and 32 guests