That's the thing, you suggest inserting the _RunWaitEx() operator instead of Run() into the so-called outer loop, because initially for me, as well as in the example above, Run() is in the outer loop. But this does not solve the problem of hanging. I repeat, in my program the Sleep() operator was initially in the outer loop already.
I repeat, the problem is solved if and only if the sleep delay is located in the inner loop, near StdoutRead()
Ok, to make it clearer or, as we say: "Instead of thousand words" - two versions of my code. Explanation - I have two gateways in the local network and I want to see online which gateway is currently enabled. Below are two versions of the program. The 1st code - hangs after a random time, the 2nd - does not.
; freezes randomly
local $fDiff=0, $pid=0, $hTimer=0, $line=""
While 1
$pid = Run("ipconfig", "", @SW_HIDE, 0x2)
$line = ""
$hTimer = TimerInit()
While 1
$fDiff = TimerDiff($hTimer)
$line &= StdoutRead($pid)
If @error or $fDiff>50 Then ExitLoop
Wend
if StringInStr($line,'0.1'&@CRLF)>1 then TraySetIcon("icon1.ico")
if StringInStr($line,'0.2'&@CRLF)>1 then TraySetIcon("icon2.ico")
sleep(20000)
WEnd
; works fine
local $fDiff=0, $pid=0, $hTimer=0, $line=""
While 1
$pid = Run("ipconfig", "", @SW_HIDE, 0x2)
$line = ""
$hTimer = TimerInit()
While 1
$fDiff = TimerDiff($hTimer)
sleep(20)
$line &= StdoutRead($pid)
If @error or $fDiff>50 Then ExitLoop
Wend
if StringInStr($line,'0.1'&@CRLF)>1 then TraySetIcon("icon1.ico")
if StringInStr($line,'0.2'&@CRLF)>1 then TraySetIcon("icon2.ico")
sleep(20000)
WEnd
The parameter with the timer, as an additional condition for exiting the cycle = 50 - was selected experimentally. In principle, it was possible to do without this condition, but just in case, this was also done at the stage of searching for a hang and remained so.
Theoretically, you can do without the timer, and then the program will take on an even more compact form.
; maybe works fine
local $pid=0, $line=""
While 1
$pid = Run("ipconfig", "", @SW_HIDE, 0x2)
$line = ""
While 1
sleep(20)
$line &= StdoutRead($pid)
If @error Then ExitLoop
Wend
if StringInStr($line,'0.1'&@CRLF)>1 then TraySetIcon("icon1.ico")
if StringInStr($line,'0.2'&@CRLF)>1 then TraySetIcon("icon2.ico")
sleep(20000)
WEnd