Jump to content

While Loop help


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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
Link to comment
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

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...