Sign in to follow this  
Followers 0
astrax

While Loop help

8 posts in this topic

Hi, my code is below. My intention was to do a ping by clicking the button $Ping_Bnt and capture the stdout into a text box $PING_TXT and break out of a loop and then will be able to click the button to start over again. Can anyone please modify so that i'm able to do so? I can ping only once and my GUI is locked because the loop is still running. Please help. Greatly appreciated

While 1

Switch GUIGetMsg()

Case $GUI_EVENT_CLOSE

ExitLoop

Case $Ping_Bnt

$ping = Run(@ComSpec & " /c ping google.com", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1

$lineout = StdoutRead($ping)

If @Error = -1 Then ExitLoop

GUICtrlSetData($PING_TXT, $lineout, 1)

WEnd

EndSwitch

WEnd

Share this post


Link to post
Share on other sites



astrax,

Just add another ExitLoop after you have set $PING_TXT:

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Ping_Bnt
            $ping = Run(@ComSpec & " /c ping google.com", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
            While 1
                $lineout = StdoutRead($ping)
                If @error = -1 Then ExitLoop
                GUICtrlSetData($PING_TXT, $lineout, 1)
                ExitLoop ; <<<<<<<<<<<<<<<<<<<<<   Added
            WEnd
    EndSwitch
WEnd

That way you exit the inner While...WEnd loop and reenter the outer While...WEnd loop ready for another button press.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

astrax,

Just add another ExitLoop after you have set $PING_TXT:

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Ping_Bnt
            $ping = Run(@ComSpec & " /c ping google.com", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
            While 1
                $lineout = StdoutRead($ping)
                If @error = -1 Then ExitLoop
                GUICtrlSetData($PING_TXT, $lineout, 1)
                ExitLoop; <<<<<<<<<<<<<<<<<<<<<   Added
            WEnd
    EndSwitch
WEnd

That way you exit the inner While...WEnd loop and reenter the outer While...WEnd loop ready for another button press.

M23

Putting the EndLoop where you said will not work unless i set a sleep time so the ping can catch up. The while loop keep looping so it picks up immediately. You can run the script to see what i mean. Try testing with a non working host like the one that has ping blocked. You won't be able to see all the result because the ping takes longer and the EndLoop already executed

#include <GuiConstantsEx.au3>
#include <ClipBoard.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GuiEdit.au3>
#include <Misc.au3>
#include <Constants.au3>

Opt('MustDeclareVars', 1)


Global $PING_TXT, $lineout, $ping
_Main()


Func _Main()
    Local $Ping_Bnt


    GUICreate("Text", 382, 280)
    
    $PING_TXT = GUICtrlCreateEdit("", 15, 10, 352, 200)

    $Ping_Bnt = GUICtrlCreateButton("Ping", 15, 220, 50)


    GUISetState()


While 1
        Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
                ExitLoop

        Case $Ping_Bnt  
        $ping = Run(@ComSpec & " /c ping google.com", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
        While 1
        ;sleep(3000)
            $lineout = StdoutRead($ping)
            If @Error = -1 Then ExitLoop
            GUICtrlSetData($PING_TXT, $lineout, 1)
        ;sleep(6000)
        ;ExitLoop
        WEnd
        
        EndSwitch
    WEnd


EndFunc;==>_Main
Edited by astrax

Share this post


Link to post
Share on other sites

astrax,

Apologies - my misunderstanding.

How about checking that something has been returned and then exiting:

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Ping_Bnt
            $ping = Run(@ComSpec & " /c ping google.com", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
            While 1
                $lineout = StdoutRead($ping)
                If @error = -1 Then ExitLoop
                If $lineout <> "" Then
                    GUICtrlSetData($PING_TXT, $lineout, 1)
                    ExitLoop
                EndIf  
            WEnd
    EndSwitch
WEnd

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This is where i'm stuck, i don't know what to do. I need all the ping stdout entries in the text box wether ping success or ping fail. I need to break the loop so i can ping again

astrax,

Apologies - my misunderstanding.

How about checking that something has been returned and then exiting:

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Ping_Bnt
            $ping = Run(@ComSpec & " /c ping google.com", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
            While 1
                $lineout = StdoutRead($ping)
                If @error = -1 Then ExitLoop
                If $lineout <> "" Then
                    GUICtrlSetData($PING_TXT, $lineout, 1)
                    ExitLoop
                EndIf  
            WEnd
    EndSwitch
WEnd

M23

Edited by astrax

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I just changed your one line:

If @Error = -1 Then ExitLoop

To:

If @Error <> 0 Then ExitLoop

Worked fine for me after that...still trying to figure out why though lol :D

Edit: Added info...

...Maybe because StdOutRead sets @error to non-zero when EOF is reached

Edited by MrMitchell

Share this post


Link to post
Share on other sites

Hi, my code is below. My intention was to do a ping by clicking the button $Ping_Bnt and capture the stdout into a text box $PING_TXT and break out of a loop and then will be able to click the button to start over again. Can anyone please modify so that i'm able to do so? I can ping only once and my GUI is locked because the loop is still running. Please help. Greatly appreciated:

While 1
        Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
                ExitLoop

        Case $Ping_Bnt  
        $ping = Run(@ComSpec & " /c ping google.com", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
        While 1
        $lineout = StdoutRead($ping)
            If @Error = -1 Then ExitLoop
            GUICtrlSetData($PING_TXT, $lineout, 1)
        WEnd

        EndSwitch
WEnd
Please post code in code tags so it is readable.

You shouldn't be testing "If @error = -1". The help file under StdOutRead() says:

Failure: Sets @error to non-zero if EOF is reached, STDOUT was not redirected for the process or other error.

Just use:
$lineout = StdoutRead($ping)
            If @Error Then ExitLoop

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

You guys are AWSOME!!! :o Thank you so much for making it work.. I've been scratching my head for hours. Thank you so much all for trying to help me one way or another.

Please post code in code tags so it is readable.

You shouldn't be testing "If @error = -1". The help file under StdOutRead() says:

Just use:

$lineout = StdoutRead($ping)
            If @Error Then ExitLoop

:D

Share this post


Link to post
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
Sign in to follow this  
Followers 0