Jump to content

FOR loop


chell
 Share

Recommended Posts

Been using autoit for a while now and this is the first time i get stuck without any ideas what so ever. So thought I'd ask for help :)

Basically what i need help with is getting out of a FOR loop with the push of a button from the GUI.

Well the code explains it better.

#include <GUIConstants.au3>

$window = GUICreate("Pinger", 300, 200, -1, -1)
GUICtrlSetBkColor($window,0x000000)

GuiSetState(@SW_SHOW)

$progress = GUICtrlCreateProgress (30,70,200,20)
$button = GUICtrlCreateButton ("Start",90,150,100,20)
$status = GUICtrlCreateLabel ("N/A",  100, 30, 80, 20, $SS_CENTER)
GUICtrlSetBkColor(-1,0x999999)
$color = GUICtrlSetColor(-1,0xff0000)
$ip = IniRead("targetip.ini", "host", "ip", "targetip.ini error")


$s = 0
main()

func main()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit(1)
        Case $msg = $button
            start()
EndSelect
Wend
EndFunc

Func start()

GUICtrlSetData ($button,"Stop")

$stat = "Offline"
$var = Ping($ip,250)

If $var Then 
    online()
Else
        $m = GUIGetMsg ()
        GUICtrlSetData ($status,$stat)
        For $i = $s To 100
        GUICtrlSetData ($progress,$i)
        Sleep(200)
                                                      ;This is where the loop just keeps going
        Next 
            if $i >100 then
                $s=0
                start()
            EndIf
EndIf
EndFunc

Func online()
    $stat = "Online"
GUICtrlSetData ($button,"Start")
GUICtrlSetData ($status,$stat)
SoundPlay("online.wav")
main()
EndFunc

Func stop()
$s = 0
GUICtrlSetData ($button,"Start")
GUICtrlSetData ($status,$stat & " [Stopped]")
main()
EndFunc

I tried using solutions from other posts If statements, ONevent, Do until. Nothing seems to work. Not sure if I did it the right way tho :whistle:

I know there's a way to do it with hotkeys but I want that to be the last resort.

Thx in advance.

Link to comment
Share on other sites

insert Exitloop

F@m!ly Guy Fr33k! - Avatar speaks for itself__________________________________________________________________________________________ite quotes... - Is your refrigerator running? If it is, It probably runs like you...very homosexually - Christians don't believe in gravity - Geeze Brian where do you think you are, Payless?- Show me potato Salad!__________________________________________________________________________________________Programs available - Shutdown timer[indent][/indent]
Link to comment
Share on other sites

Been using autoit for a while now and this is the first time i get stuck without any ideas what so ever. So thought I'd ask for help :)

Basically what i need help with is getting out of a FOR loop with the push of a button from the GUI.

Well the code explains it better.

#include <GUIConstants.au3>

$window = GUICreate("Pinger", 300, 200, -1, -1)
GUICtrlSetBkColor($window,0x000000)

GuiSetState(@SW_SHOW)

$progress = GUICtrlCreateProgress (30,70,200,20)
$button = GUICtrlCreateButton ("Start",90,150,100,20)
$status = GUICtrlCreateLabel ("N/A",  100, 30, 80, 20, $SS_CENTER)
GUICtrlSetBkColor(-1,0x999999)
$color = GUICtrlSetColor(-1,0xff0000)
$ip = IniRead("targetip.ini", "host", "ip", "targetip.ini error")
$s = 0
main()

func main()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit(1)
        Case $msg = $button
            start()
EndSelect
Wend
EndFunc

Func start()

GUICtrlSetData ($button,"Stop")

$stat = "Offline"
$var = Ping($ip,250)

If $var Then 
    online()
Else
        $m = GUIGetMsg ()
        GUICtrlSetData ($status,$stat)
        For $i = $s To 100
        GUICtrlSetData ($progress,$i)
        Sleep(200)
                                        ;This is where the loop just keeps going
        Next 
            if $i >100 then
                $s=0
                start()
            EndIf
EndIf
EndFunc

Func online()
    $stat = "Online"
GUICtrlSetData ($button,"Start")
GUICtrlSetData ($status,$stat)
SoundPlay("online.wav")
main()
EndFunc

Func stop()
$s = 0
GUICtrlSetData ($button,"Start")
GUICtrlSetData ($status,$stat & " [Stopped]")
main()
EndFunc

I tried using solutions from other posts If statements, ONevent, Do until. Nothing seems to work. Not sure if I did it the right way tho :whistle:

I know there's a way to do it with hotkeys but I want that to be the last resort.

Thx in advance.

A couple of things to keep in mind

1) Don't call functions from within the same function in order to "restart" or otherwise loop that function. Also, calling a function which then calls the same function which called it should be avoided. If you do this then your script will inevitably crash after i has run long enough. There are other ways of obtaining the desired results, so keep experimenting until you find them.

Example using excerpt from your code

Func start()
...
if $i >100 then
    $s=0
    start()
EndIf
EndFunc
oÝ÷ Ù.².Ö¯¢'m+7ÝKjZ(¦)ì×­çîËb¢x§²×w}öN±ÛazZ(§^*.¦ë!i»­¶ò¢ì"YÞyÛhvp¢Øbç$~ƺ)²Æ zƧvØ^ë­.)bj{¬zØ^:q/z{f¡×­¡Ú"µØ}êÞÙrnëH¦·z»ayø«²Ù¶jëh×6GUICtrlSetData ($button,"Stop")
For $i = $s To 100
    Sleep(200)
    $msg = GUIGetMsg()
    if $msg=$button then ExitLoop
Next 
oÝ÷ Øù^jÇ¢×­«bÝz»h¦·¬z»Þ¶¡jwezÊÊ«è¢{k¢["Èy§îËb¢z-êí©íê'z)ð'!¶²ÁêÞr·µçr¢éÞyÛh!·¥ëÞºÇ Úö«¦åzÍ÷jëh×6Global $button = GUICtrlCreateButton ("Start",90,150,100,20)

I could be wrong on this last part, but that is my assumption

Edited by improbability_paradox
Link to comment
Share on other sites

A couple of things to keep in mind

1) Don't call functions from within the same function in order to "restart" or otherwise loop that function. Also, calling a function which then calls the same function which called it should be avoided. If you do this then your script will inevitably crash after i has run long enough. There are other ways of obtaining the desired results, so keep experimenting until you find them.

Example using excerpt from your code

Func start()
...
if $i >100 then
    $s=0
    start()
EndIf
EndFunc
oÝ÷ Ù.².Ö¯¢'m+7ÝKjZ(¦)ì×­çîËb¢x§²×w}öN±ÛazZ(§^*.¦ë!i»­¶ò¢ì"YÞyÛhvp¢Øbç$~ƺ)²Æ zƧvØ^ë­.)bj{¬zØ^:q/z{f¡×­¡Ú"µØ}êÞÙrnëH¦·z»ayø«²Ù¶jëh×6GUICtrlSetData ($button,"Stop")
For $i = $s To 100
    Sleep(200)
    $msg = GUIGetMsg()
    if $msg=$button then ExitLoop
Next 
oÝ÷ Øù^jÇ¢×­«bÝz»h¦·¬z»Þ¶¡jwezÊÊ«è¢{k¢["Èy§îËb¢z-êí©íê'z)ð'!¶²ÁêÞr·µçr¢éÞyÛh!·¥ëÞºÇ Úö«¦åzÍ÷jëh×6Global $button = GUICtrlCreateButton ("Start",90,150,100,20)

I could be wrong on this last part, but that is my assumption

Thx alot for your help. It's working fine now :whistle:

if $m=$button then ExitLoop & stop()

That line made it all clear, i did that before but got stuck at "Where tha hell do i put the endif" lol sry :">

TYYYYYYYYYY you saved my script, was heading for the trashcan. :)

Here's how it looks now..

Global $stat

#include <GUIConstants.au3>

$window = GUICreate("Pinger", 300, 200, -1, -1)
GUICtrlSetBkColor($window,0x000000)

GuiSetState(@SW_SHOW)

Global $progress = GUICtrlCreateProgress (30,70,200,20)
Global $button = GUICtrlCreateButton ("Start",90,150,100,20)
Global $status = GUICtrlCreateLabel ("N/A",  100, 30, 80, 20, $SS_CENTER)
GUICtrlSetBkColor(-1,0x999999)
Global $color = GUICtrlSetColor(-1,0xff0000)
Global $ip = IniRead("targetip.ini", "host", "ip", "targetip.ini error")


$s = 0
main()

func main()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit(1)
        Case $msg = $button
            start()
EndSelect
Wend
EndFunc

Func start()


GUICtrlSetData ($button,"Stop")

$stat = "Offline"
While 1
$var = Ping($ip,250)

If $var Then
    ExitLoop
    online()
Else
        GUICtrlSetData ($status,$stat)
        For $i = $s To 100
        GUICtrlSetData ($progress,$i)
        Sleep(200)
        $m = GUIGetMsg()
            if $m=$button then ExitLoop & stop()
            
        Next
            if $i >100 then
                $s=0
            EndIf
EndIf
WEnd
EndFunc

stop()

Func online()
    $stat = "Online"
GUICtrlSetData ($button,"Start")
GUICtrlSetData ($status,$stat)
SoundPlay("online.wav")
main()
EndFunc

Func stop()
GUICtrlSetData ($button,"Start")
GUICtrlSetData ($status,$stat & " [Stopped]")
main()
EndFunc
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...