Jump to content

Recommended Posts

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
Posted

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.

Posted

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
Posted

#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

Posted

#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

Posted

#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

Posted

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

Posted

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
Posted

#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

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
×
×
  • Create New...