Jump to content

Looping question - Once again


Recommended Posts

Hello,

I'm new in AutoIT and unfortunately I run into some trouble. I know there is dozens and hundreds of topics like this, and I really did use search and read wiki... Nothing seems to work for me. Usually I figure out thing by myself, but this one got me... So we are talking about an infinite loop terminating. I want the main script stay open and run forward, I just want the loop be stopped by a button. So call for exit is out I guess. Here is a small example what is in my mind:

#include <GuiConstantsEx.au3>

_GUI()

Func _GUI()

    $ParentWin = GUICreate("Window", 150, 65)

    $startbutton = GUICtrlCreateButton("Start", 5, 5, 140, 25)
    GUICtrlSetState(-1, $GUI_FOCUS)
    $stopbutton  = GUICtrlCreateButton("Stop", 5, 35, 140, 25)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUISetState()

    While 1

        $msg = GUIGetMsg()

        Select

            Case $msg = $GUI_EVENT_CLOSE

                ExitLoop

            Case $msg = $startbutton

                _Loop()
                GUICtrlSetState($startbutton, $GUI_DISABLE)
                GUICtrlSetState($stopbutton,  $GUI_ENABLE)

            Case $msg = $stopbutton

                ; Terminate _Loop() ?
                GUICtrlSetState($stopbutton,  $GUI_DISABLE)
                GUICtrlSetState($startbutton, $GUI_ENABLE)

        EndSelect

    WEnd

    GUIDelete()

    Exit

EndFunc

Func _Loop()

    $i = 0

    While 1

        $i += 1
        MsgBox(0, "MsgBox - " & $i, "Text - " & $i)
        Sleep(5000)

    WEnd

EndFunc

The whole program becomes unresponsive, You can only clicking the MsgBox...

I can't find a solution... I really did try hard. I compiled the looping part, started the exe with a button and killed the processes with the other, but it seems a bit barbaric ;) Even so, it would be one good way to do this, but the looping processes leave behind the icon on the tray and that's just epic :) If this is the only solution for me, can I run the loop.exe in a way so it won't put icon to tray?

Link to comment
Share on other sites

You can use hotkey to stop loop

HotKeySet("{esc}","ExitLoopa")

$aa=0

_Loop()

Func ExitLoopa()

$aa=1

EndFunc

Func _Loop()

$i = 0

While 1

If $aa=1 Then ExitLoop

$i += 1

MsgBox(0, "MsgBox - " & $i, "Text - " & $i)

Sleep(5000)

WEnd

EndFunc

Edited by dot
Link to comment
Share on other sites

You're not processing the message queue inside the infinite loop of the Loop() function.

So, there is no means to process a $stopbutton event.

Some possibilities are:

1. Switch to GUIOnEventMode (which I'm personally not very fond of)

2. Add a GUIGetMsg() statement and process the $stopbutton event in the Loop() function.

3. Use a flag to control execution of the Loop() function from your main control loop (this may be the more 'structured' way to go)

Option #3 might look something like:

#include <GuiConstantsEx.au3>

Global $Loop_Enabled, $i
_GUI()

Func _GUI()
    $ParentWin = GUICreate("Window", 150, 65)
    $startbutton = GUICtrlCreateButton("Start", 5, 5, 140, 25)
    GUICtrlSetState(-1, $GUI_FOCUS)
    $stopbutton  = GUICtrlCreateButton("Stop", 5, 35, 140, 25)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $startbutton
                GUICtrlSetState($startbutton, $GUI_DISABLE)
                GUICtrlSetState($stopbutton,  $GUI_ENABLE)
                $Loop_Enabled = 1 ; Start loop
            Case $msg = $stopbutton
                GUICtrlSetState($stopbutton,  $GUI_DISABLE)
                GUICtrlSetState($startbutton, $GUI_ENABLE)
                $Loop_Enabled = 0 ; Terminate loop
        EndSelect
        If $Loop_Enabled Then _Loop()
    WEnd
    GUIDelete()
    Exit
EndFunc

Func _Loop()
    $i += 1
    ToolTip("Text - " & $i)
EndFunc
Edited by Spiff59
Link to comment
Share on other sites

#include <GuiConstantsEx.au3>
 Dim $i
_GUI()

Func _GUI()

    $ParentWin = GUICreate("Window", 150, 65)

    $startbutton = GUICtrlCreateButton("Start", 5, 5, 140, 25)
    GUICtrlSetState(-1, $GUI_FOCUS)
    $stopbutton  = GUICtrlCreateButton("Stop", 5, 35, 140, 25)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUISetState()

    While 1

        $msg = GUIGetMsg()

        Select

            Case $msg = $GUI_EVENT_CLOSE

                ExitLoop

            Case $msg = $startbutton
 AdlibRegister('_Loop', 5000)

;~                 _Loop()
                GUICtrlSetState($startbutton, $GUI_DISABLE)
                GUICtrlSetState($stopbutton,  $GUI_ENABLE)

            Case $msg = $stopbutton
AdlibUnRegister('_Loop')

                ; Terminate _Loop() ?
                GUICtrlSetState($stopbutton,  $GUI_DISABLE)
                GUICtrlSetState($startbutton, $GUI_ENABLE)

        EndSelect

    WEnd

    GUIDelete()

    Exit

EndFunc

Func _Loop()

  

   

        $i += 1
        MsgBox(0, "MsgBox - " & $i, "Text - " & $i)
;~         Sleep(5000)

   

EndFunc

Edited by dot
Link to comment
Share on other sites

You're not processing the message queue inside the infinite loop of the Loop() function.

So, there is no means to process a $stopbutton event.

Some possibilities are:

1. Switch to GUIOnEventMode (which I'm personally not very fond of)

2. Add a GUIGetMsg() statement and process the $stopbutton event in the Loop() function.

3. Use a flag to control execution of the Loop() function from your main control loop (this may be the more 'structured' way to go)

Option #3 might look something like:

#include <GuiConstantsEx.au3>

Global $Loop_Enabled, $i
_GUI()

Func _GUI()
    $ParentWin = GUICreate("Window", 150, 65)
    $startbutton = GUICtrlCreateButton("Start", 5, 5, 140, 25)
    GUICtrlSetState(-1, $GUI_FOCUS)
    $stopbutton  = GUICtrlCreateButton("Stop", 5, 35, 140, 25)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $startbutton
                GUICtrlSetState($startbutton, $GUI_DISABLE)
                GUICtrlSetState($stopbutton,  $GUI_ENABLE)
                $Loop_Enabled = 1 ; Start loop
            Case $msg = $stopbutton
                GUICtrlSetState($stopbutton,  $GUI_DISABLE)
                GUICtrlSetState($startbutton, $GUI_ENABLE)
                $Loop_Enabled = 0 ; Terminate loop
        EndSelect
        If $Loop_Enabled Then _Loop()
    WEnd
    GUIDelete()
    Exit
EndFunc

Func _Loop()
    $i += 1
    ToolTip("Text - " & $i)
EndFunc

Thank You very-very much. This is exactly what I'm looking for. I would never figure it out in 100 years ;) You should consider put this code in to the FAQ's :) And to the main program example folder ;)

Thank You one more time.

Have a nice day.

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