Jump to content

STDOUT not captured occasionally


Recommended Posts

 I have having issues capture the STDOUT output from applications. I use the code below to run several different exe files and about 1% of the time, the STDOUT is empty and It falls into the "retry" loop ($bRetry = TRUE) and tries a second time. It works on the first try most of the time.

Is there anything that I am doing wrong?

 

 

Local $sOutput = RunAndDetectTimeout("C:\TestAutoIt\devcon.exe" & $sDevcon_Params, "", @SW_HIDE, $STDOUT_CHILD, 10)

 

Func RunAndDetectTimeout($sApp, $sParam1, $Param2, $Param3, $iTimeout)
     Local $bRetryAttempts = 0
     Local $bRetry = False
     Local $sOutput = ""

    Do
        $iPID = Run($sApp, $sParam1, $Param2, $Param3)
        sleep(500)
       
        Local $PWC = ProcessWaitClose($iPID,$iTimeout)
        If $PWC == 0 Then
             ProcessClose($iPID)
            $bRetryAttempts = $bRetryAttempts + 1
            $bRetry = True
        Else
            $bRetry = False
        EndIf
        sleep(500)
     
        $sOutput = StdoutRead($iPID)
        sleep(500)
     
        StdioClose($iPID)
        If $sOutput == "" Then
            If $bRetry == False Then
                $bRetryAttempts = $bRetryAttempts + 1
                $bRetry = True
            EndIf

        EndIf
    Until $bRetryAttempts > 1 Or $bRetry == False

     Return $sOutput
 EndFunc 

Link to comment
Share on other sites

maybe the problem of the stdout output loss is due to the fact that you use StdoutRead() after the close of the process. I think that is not a good idea doing so.
You can try the little variation of your scrpt  (not tested) that uses the StdoutRead() while the process is still alive. The timeout is controlled by the TimerInit & TimerDiff pair

Local $sOutput = RunAndDetectTimeout("C:\TestAutoIt\devcon.exe" & $sDevcon_Params, "", @SW_HIDE, $STDOUT_CHILD, 10)

Func RunAndDetectTimeout($sApp, $sParam1, $Param2, $Param3, $iTimeout)
    Local $bRetryAttempts = 0
    Local $bRetry = False
    Local $sOutput = ""
    Local $iTimer, $PWC = 1

    Do
        $iPID = Run($sApp, $sParam1, $Param2, $Param3)
        Sleep(500)

        ; Local $PWC = ProcessWaitClose($iPID, $iTimeout)
        $iTimer = TimerInit()
        While 1
            $sOutput &= StdoutRead($iPID)
            If @error Then ExitLoop ; process ended successfully
            If TimerDiff($iTimer) > $iTimeout  * 1000 Then ; timeout
                $PWC = 0 ; timeout flag
                ExitLoop
            EndIf
            Sleep(250)
        WEnd

        If $PWC == 0 Then
            ProcessClose($iPID)
            $bRetryAttempts = $bRetryAttempts + 1
            $bRetry = True
        Else
            $bRetry = False
        EndIf
        Sleep(500)

        ; $sOutput = StdoutRead($iPID)
        ; Sleep(500)

        StdioClose($iPID)
        If $sOutput == "" Then
            If $bRetry == False Then
                $bRetryAttempts = $bRetryAttempts + 1
                $bRetry = True
            EndIf

        EndIf
    Until $bRetryAttempts > 1 Or $bRetry == False

    Return $sOutput
EndFunc   ;==>RunAndDetectTimeout

 

Edited by Chimp
corrected TimerDiff() timeout calculation

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...