Sign in to follow this  
Followers 0
rojiblanco

Help me function contains while true loop !

10 posts in this topic

#1 ·  Posted (edited)

i create a GUI ,contains two button(called button1 and button2),when i click button1 the program will call a function(called function1) which contains a while true loop

Now i want when i click button2 , the program will terminate function1.

So could i do it?

If I can ,you can tell me how to do it?

Please

Thanks alot!

Edited by rojiblanco

Share this post


Link to post
Share on other sites



AutoIt can only work on one button at a time. You will need to make a helper function to do what you want.

While 1
    Sleep(100)
    If $run Then Go()
WEnd

Func GoHelper()
    $run = True
EndFunc

Func Go()
    DO STUFF HERE THAT CHECKS $terminate
End Func

Func Terminate()
    $terminate = True
EndFunc

Your GUI should call GoHelper and Terminate. Hope this helps.

Share this post


Link to post
Share on other sites

thanks ,this is my example code

#include <GUIConstantsEx.au3>

    Local $Button_1, $Button_2, $msg
    GUICreate("My GUI Button") 
    $Button_1 = GUICtrlCreateButton("Button_1", 10, 30, 100)
    $Button_2 = GUICtrlCreateButton("Button_2", 120, -1)

    GUISetState()    
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Button_1
                _Function1()
            Case $msg = $Button_2
                MsgBox(0, 'Testing', 'Button 2 was pressed')    
        EndSelect
    WEnd
Func _Function1()
    While 1
        consoleWrite("running" &@CRLF)
        Sleep(5000)
    WEnd
EndFunc

Share this post


Link to post
Share on other sites

#include <GUIConstantsEx.au3>

Local $Button_1, $Button_2, $msg
GUICreate("My GUI Button")
$Button_1 = GUICtrlCreateButton("Button_1", 10, 30, 100)
$Button_2 = GUICtrlCreateButton("Button_2", 120, -1)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            AdlibEnable("_Function1", 5000)
        Case $msg = $Button_2
            MsgBox(0, 'Testing', 'Button 2 was pressed')
    EndSelect
WEnd

Func _Function1()
    ConsoleWrite("running" & @CRLF)
EndFunc   ;==>_Function1

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#include <GUIConstantsEx.au3>

Local $Button_1, $Button_2, $msg
GUICreate("My GUI Button")
$Button_1 = GUICtrlCreateButton("Button_1", 10, 30, 100)
$Button_2 = GUICtrlCreateButton("Button_2", 120, -1)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            AdlibEnable("_Function1", 5000)
        Case $msg = $Button_2
            MsgBox(0, 'Testing', 'Button 2 was pressed')
    EndSelect
WEnd

Func _Function1()
    ConsoleWrite("running" & @CRLF)
EndFunc   ;==>_Function1

8)

oh yeah thanks alot!! Great Man!!!!:P

Share this post


Link to post
Share on other sites

#include <GUIConstantsEx.au3>

GUICreate("My GUI Button")
$Button_1 = GUICtrlCreateButton("Button_1", 10, 30, 100)
$Button_2 = GUICtrlCreateButton("Button_2", 120, -1)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            _Function1()
    EndSelect
WEnd

Func _Function1()
    While 1
        If GUIGetMsg() = $Button_2 Then Return
        ConsoleWrite("running" & @CRLF)
        Sleep(100)
    WEnd
EndFunc

Share this post


Link to post
Share on other sites

thanks Zedna about your solution

So if in while loop have so many operators and it will work in several minutes. So i want when the button2 is clicked the fucntion1 will stop immediatly or 1,2 seconds .

Follows your solution ,at the time i clicked, in case while loop not at IF conditions so, it will not stop as i wish.

Thanks alot

Share this post


Link to post
Share on other sites

thanks Zedna about your solution

So if in while loop have so many operators and it will work in several minutes. So i want when the button2 is clicked the fucntion1 will stop immediatly or 1,2 seconds .

Follows your solution ,at the time i clicked, in case while loop not at IF conditions so, it will not stop as i wish.

Thanks alot

#include <GUIConstantsEx.au3>

Global $was_stop = 0

GUICreate("My GUI Button")
$Button_1 = GUICtrlCreateButton("Button_1", 10, 30, 100)
$Button_2 = GUICtrlCreateButton("Button_2", 120, -1)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            _Function1()
    EndSelect
WEnd

Func _Function1()
    $was_stop = 0
    While 1
        If IsStop() Then Return
        ConsoleWrite("running" & @CRLF)
        ; some code 1
        ; ...
        If IsStop() Then Return
        ; some code 2
        ; ...
        If IsStop() Then Return
        Sleep(100)
    WEnd
EndFunc

Func IsStop()
    If $was_stop Then Return 1
    If GUIGetMsg() = $Button_2 Then
        $was_stop = 1
        Return 1
    EndIf
    Return 0
EndFunc

Share this post


Link to post
Share on other sites

#include <GUIConstantsEx.au3>

Global $was_stop = 0

GUICreate("My GUI Button")
$Button_1 = GUICtrlCreateButton("Button_1", 10, 30, 100)
$Button_2 = GUICtrlCreateButton("Button_2", 120, -1)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            _Function1()
    EndSelect
WEnd

Func _Function1()
    $was_stop = 0
    While 1
        If IsStop() Then Return
        ConsoleWrite("running" & @CRLF)
        ; some code 1
        ; ...
        If IsStop() Then Return
        ; some code 2
        ; ...
        If IsStop() Then Return
        Sleep(100)
    WEnd
EndFunc

Func IsStop()
    If $was_stop Then Return 1
    If GUIGetMsg() = $Button_2 Then
        $was_stop = 1
        Return 1
    EndIf
    Return 0
EndFunc
Thanks

It is good solution :P

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