Jump to content

Recommended Posts

Posted (edited)

I had a need to create a "ping.exe" monitoring function.
Here is the result:

#include <AutoItConstants.au3>
#include <Debug.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

_DebugSetup('Check Ping - press SHIFT KEY to close')

If @ScriptName = 'Ping.exe' Then Exit ; compiled script should not be named "Ping.exe" as this is used for Run() and this can lead to recursion

OnAutoItExitRegister(_Adlib_Ping_Exit)
AdlibRegister(_Adlib_Ping)

_Example()
Func _Example()
    Local $hDLL = DllOpen('user32.dll')

    While 1
        Sleep(10)
        If _IsPressed('10', $hDLL) Then ExitLoop
    WEnd

EndFunc   ;==>_Example

Func _Adlib_Ping()
    Local Static $iPID = Run('ping www.google.com -l 1024 -t', @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    Local Static $RUN_ONCE = __Adlib_Ping_Exit($iPID)
    #forceref $RUN_ONCE

    Local Static $sOutput = '' ; Store the output of StdoutRead to a variable.

    Local Static $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.

    $sOutput &= StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run.
    If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
        _DebugOut('Ping.exe process (' & $iPID & ') not exist ? ')
        AdlibUnRegister(_Adlib_Ping)
    EndIf
    If StringRegExp($sOutput, '(?is)(\R)', $STR_REGEXPMATCH) Then
        If TimerDiff($hTimer) > 5000 Then
            _DebugOut(@HOUR & ':' & @MIN & ':' & @SEC & ' PING RESULT = ' & $sOutput)
            $hTimer = TimerInit()
        EndIf
        $sOutput = ''
    EndIf

EndFunc   ;==>_Adlib_Ping

Func _Adlib_Ping_Exit()
    __Adlib_Ping_Exit()
EndFunc   ;==>_Adlib_Ping_Exit

Func __Adlib_Ping_Exit($vParameter = Default)
    Local Static $vParameter_static = $vParameter
    If Not @Compiled Then ConsoleWrite('- ' & @ScriptLineNumber & ' Parameter=' & $vParameter & ' Static=' & $vParameter_static & @CRLF)
    If $vParameter = Default And IsInt($vParameter_static) Then
        ProcessClose($vParameter_static)
        If Not @Compiled Then ConsoleWrite('! CLOSE PORCESS' & @CRLF)
        Local $hWndReportWindow = WinGetHandle($__g_sReportTitle_Debug, $__g_sReportWindowText_Debug)
        GUIDelete($hWndReportWindow)
    ElseIf IsInt($vParameter) Then
        $vParameter_static = $vParameter
    EndIf
EndFunc   ;==>__Adlib_Ping_Exit

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 5/25/2020 at 9:34 PM, TheDcoder said:

@mLipok A suggestion to improve efficiency of the main loop, use HotKeySet to modify a variable (probably a boolean) and check that instead of calling _IsPressed :)

Expand  

Main loop is only an example and has nothing to do with efficiency.

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)
  On 5/25/2020 at 9:23 PM, argumentum said:

Would you explain at length what need this solves ?

Expand  

This following improved example will explain this.

;~ https://www.autoitscript.com/forum/topic/202843-pingexe-monitoring-function/

#include <AutoItConstants.au3>
#include <Debug.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

_DebugSetup('Check Ping - press SHIFT KEY to close')
OnAutoItExitRegister(__DebugClose)

If @ScriptName = 'Ping.exe' Then Exit ; compiled script should not be named "Ping.exe" as this is used for Run() and this can lead to recursion

_Example()

Func _INIT_PING_TESTING()
    _Adlib_Ping_SetURL('www.onet.pl')
    AdlibRegister(_Adlib_Ping_Wrapper)
EndFunc   ;==>_INIT_PING_TESTING

Func _Example()
    ; this example function is only for simulation of looping some activieties
    Local $hDLL = DllOpen('user32.dll')

    ; here we use ForNext Loop, as a way to simulate some data processing or GUI maintaining
    For $iStep_idx = 1 To 1000
        Sleep(1000)
        If _IsPressed('10', $hDLL) Then ExitLoop
        _DebugOut('Step #' & $iStep_idx & ' Some processing.....')
        If $iStep_idx = 11 Then _INIT_PING_TESTING() ; here we simulates a situation when USER starts testing PING
        If $iStep_idx = 100 Then _Adlib_Ping_Exit_Wrapper() ; here we simulates a situation when USER stop testing PING
    Next

    ; REMARKS
    ; PING testing is processed in the "background" as a Adlib
    ; the basic premise is to interleaves PING logs with logs from the main program loop.

EndFunc   ;==>_Example

Func _Adlib_Ping_Wrapper()
    __Adlib_Ping()
EndFunc   ;==>_Adlib_Ping_Wrapper

Func _Adlib_Ping_SetURL($sURL)
    __Adlib_Ping($sURL)
EndFunc   ;==>_Adlib_Ping_SetURL

Func __Adlib_Ping($sURL = Default)
    Local Static $iPID = 0
    If IsString($sURL) And $iPID = 0 Then
        _DebugOut('Testing URL = ' & $sURL)
        $iPID = Run('ping ' & $sURL & ' -l 1024 -t', @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
        Return
    EndIf

    Local Static $RUN_ONCE_EXIT_1 = OnAutoItExitRegister(_Adlib_Ping_Exit_Wrapper)
    Local Static $RUN_ONCE_EXIT_2 = __Adlib_Ping_Exit($iPID)
    #forceref $RUN_ONCE_EXIT_1, $RUN_ONCE_EXIT_2

    Local Static $sOutput = '' ; Store the output of StdoutRead to a variable.

    Local Static $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.

    $sOutput &= StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run.
    If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
        _DebugOut('Ping.exe process (' & $iPID & ') not exist ? ')
        AdlibUnRegister(_Adlib_Ping_Wrapper)
    EndIf
    If StringRegExp($sOutput, '(?is)(\R)', $STR_REGEXPMATCH) Then
        If TimerDiff($hTimer) > 5000 Then
            _DebugOut(@HOUR & ':' & @MIN & ':' & @SEC & ' PING RESULT = ' & $sOutput)
            $hTimer = TimerInit()
        EndIf
        $sOutput = ''
    EndIf

EndFunc   ;==>__Adlib_Ping

Func _Adlib_Ping_Exit_Wrapper()
    __Adlib_Ping_Exit()
EndFunc   ;==>_Adlib_Ping_Exit_Wrapper

Func __Adlib_Ping_Exit($vParameter = Default)
    Local Static $vParameter_static = $vParameter
    If Not @Compiled Then ConsoleWrite('- ' & @ScriptLineNumber & ' Parameter=' & $vParameter & ' Static=' & $vParameter_static & @CRLF)
    If $vParameter = Default And IsInt($vParameter_static) Then
        ProcessClose($vParameter_static)
        If Not @Compiled Then ConsoleWrite('! CLOSE PORCESS' & @CRLF)
    ElseIf IsInt($vParameter) Then
        $vParameter_static = $vParameter
    EndIf
EndFunc   ;==>__Adlib_Ping_Exit

Func __DebugClose()
    Local $hWndReportWindow = WinGetHandle($__g_sReportTitle_Debug, $__g_sReportWindowText_Debug)
    GUIDelete($hWndReportWindow)
EndFunc

 

Edited by mLipok
Example changed

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
  • Recently Browsing   0 members

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