Jump to content

GUI Func & button problem


Merchants
 Share

Recommended Posts

If i run a simple GUI + 2 buttons then it works fine

but if i press a button 1 then run func test1()

func is slow & takes time so if i want to stop (while the func test1 is still running) it does not work..

any one got ideas?

#include <GUIListBox.au3>
#include <Date.au3>
Global $GUI, $msg, $hListBox
Opt("GUICoordMode", 1)
_Main()

Func _Main()
    Local $GUI, $msg, $hListBox
    $GUI = GUICreate("Test program", 300, 200)
    $hListBox = _GUICtrlListBox_Create($GUI, "", 25, 30, 255, 90)
    GuiCtrlCreateGroup("Logs",12,12,280,110)
    GuiCtrlCreateGroup("Buttons",12,130,280,60)

    $Button_1 = GuiCtrlCreateButton("Run", 30, 150, 100, 26)
    $Button_2 = GuiCtrlCreateButton("Stop and Exit", 170, 150, 100, 26)
    GUISetState()

    While 1
        $msg = GUIGetMsg($GUI)
        Select
            Case $msg = $GUI_EVENT_CLOSE
                GUIDelete($GUI)
                Exit
            Case $msg = $Button_1
                _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Running: Func test1()", 0)
                test1()
            Case $msg = $Button_2
                _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0)
                test2()
        EndSelect
    WEnd
EndFunc

Func test1()
    Run("Notepad.exe")
    WinWaitActive("[Class:Notepad]")
    Send("Running Notpad")
    Sleep(500)
    Send(".")
    Sleep(500)
    Send(".")
    Sleep(500)
    Send(".")
    Sleep(500)
    Send(".")
    Sleep(500)
    Send("{Enter}Done{!}")
    Sleep(800)
    Send("{Enter 2}Moving Notepad")
    Sleep(500)
    Send(".")
    Sleep(500)
    Send(".")
    Sleep(500)
    Send(".")
    Sleep(500)
    Send(".")
    Sleep(500)
    WinMove("[Class:Notepad]", "", 10, 10, 200, 200)
    Sleep(900)
    WinMove("[Class:Notepad]", "", 50, 50, 500, 500)
    Sleep(500)
    Send("{Enter}Done{!}")
    Sleep(1000)
    WinActivate("[Class:Notepad]")
    Send("{Enter 2}try to press stop and exit{Enter}zZzZ... 5 sec{Enter 2}")
    Sleep(5000)
    WinActivate("[Class:Notepad]")
    Send("does not work :P{Enter}zZzZ... 5 sec{Enter 2}")
    Sleep(5000)
    WinActivate("[Class:Notepad]")
    Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}")
    Sleep(5000)
    WinActivate("[Class:Notepad]")
    Send("button does not work{!}{Enter}Func test1 is still running")
    Sleep(800)
    Send(".")
    Sleep(800)
    Send(".")
    Sleep(800)
    Send(".")
    Sleep(1000)
    Send("{Enter}zZzZ... 5 sec{Enter 2}")
    Sleep(5000)
    WinActivate("[Class:Notepad]")
    Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}")
    Sleep(5000)
    WinActivate("[Class:Notepad]")
    Send("still does not work{!}{Enter}zZzZ... 5 sec{Enter 2}")
    Sleep(5000)
    WinActivate("[Class:Notepad]")
    Send("wel i wil exit now you can press stop and exit{!}")
EndFunc

Func test2()
    Sleep(2000)
    Exit
EndFunc
Edited by Merchants
Link to comment
Share on other sites

Before I try to understand what you are doing, can you explain all the Sleep() calls in test1() :mellow:? There is no need to use Sleep, you can just use WinWaitActive or WinWait if you want to be sure the window exists / is active. Ofcourse test1() is slow if you put in so many Sleep() calls...

Edited by d4ni
Link to comment
Share on other sites

  • Moderators

Merchants,

You cannot break in on a running function and stop it in mid-flow - even a HotKey will let the function continue after it does its own thing. You need to look for a flag of some sort - somrthing like this:

#include <GUIListBox.au3>
#include <Date.au3>
Global $GUI, $msg, $hListBox, $Button_2
Opt("GUICoordMode", 1)
_Main()

Func _Main()
    Local $GUI, $msg, $hListBox
    $GUI = GUICreate("Test program", 300, 200)
    $hListBox = _GUICtrlListBox_Create($GUI, "", 25, 30, 255, 90)
    GuiCtrlCreateGroup("Logs",12,12,280,110)
    GuiCtrlCreateGroup("Buttons",12,130,280,60)

    $Button_1 = GuiCtrlCreateButton("Run", 30, 150, 100, 26)
    $Button_2 = GuiCtrlCreateButton("Stop and Exit", 170, 150, 100, 26)
    GUISetState()

    While 1
        $msg = GUIGetMsg($GUI)
        Select
            Case $msg = $GUI_EVENT_CLOSE
                GUIDelete($GUI)
                Exit
            Case $msg = $Button_1
                _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Running: Func test1()", 0)
                test1()
            Case $msg = $Button_2
                _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0)
                test2()
        EndSelect
    WEnd
EndFunc

Func test1()
    If Not WinExists("[Class:Notepad]") Then Run("Notepad.exe")
    WinActivate("[Class:Notepad]")
    WinWaitActive("[Class:Notepad]")
    Send(@CRLF & @CRLF & "Running Notpad")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send("{Enter}Done{!}")
    Timer(800)
    Send("{Enter 2}Moving Notepad")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    WinMove("[Class:Notepad]", "", 10, 10, 200, 200)
    Timer(900)
    WinMove("[Class:Notepad]", "", 50, 50, 500, 500)
    If Timer(500) = 1 then Return
    Send("{Enter}Done{!}")
    If Timer(1000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("{Enter 2}try to press stop and exit{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("does not work :P{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("button does not work{!}{Enter}Func test1 is still running")
    If Timer(800) = 1 then Return
    Send(".")
    If Timer(800) = 1 then Return
    Send(".")
    If Timer(800) = 1 then Return
    Send(".")
    If Timer(1000) = 1 then Return
    Send("{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("still does not work{!}{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("wel i wil exit now you can press stop and exit{!}")
EndFunc

Func test2()
    Sleep(2000)
EndFunc

Func Timer($iTime)

    $iBegin = TimerInit()

    While TimerDiff($iBegin) < $iTime
        If GUIGetMsg() = $Button_2 Then
            _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0)
            test2()
            Return 1
        EndIf
    WEnd

EndFunc

Of course this will only work if you have lots of idle loops in the function you want to break into. :mellow:

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

Merchants,

You cannot break in on a running function and stop it in mid-flow - even a HotKey will let the function continue after it does its own thing. You need to look for a flag of some sort - somrthing like this:

#include <GUIListBox.au3>
#include <Date.au3>
Global $GUI, $msg, $hListBox, $Button_2
Opt("GUICoordMode", 1)
_Main()

Func _Main()
    Local $GUI, $msg, $hListBox
    $GUI = GUICreate("Test program", 300, 200)
    $hListBox = _GUICtrlListBox_Create($GUI, "", 25, 30, 255, 90)
    GuiCtrlCreateGroup("Logs",12,12,280,110)
    GuiCtrlCreateGroup("Buttons",12,130,280,60)

    $Button_1 = GuiCtrlCreateButton("Run", 30, 150, 100, 26)
    $Button_2 = GuiCtrlCreateButton("Stop and Exit", 170, 150, 100, 26)
    GUISetState()

    While 1
        $msg = GUIGetMsg($GUI)
        Select
            Case $msg = $GUI_EVENT_CLOSE
                GUIDelete($GUI)
                Exit
            Case $msg = $Button_1
                _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Running: Func test1()", 0)
                test1()
            Case $msg = $Button_2
                _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0)
                test2()
        EndSelect
    WEnd
EndFunc

Func test1()
    If Not WinExists("[Class:Notepad]") Then Run("Notepad.exe")
    WinActivate("[Class:Notepad]")
    WinWaitActive("[Class:Notepad]")
    Send(@CRLF & @CRLF & "Running Notpad")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send("{Enter}Done{!}")
    Timer(800)
    Send("{Enter 2}Moving Notepad")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    Send(".")
    If Timer(500) = 1 then Return
    WinMove("[Class:Notepad]", "", 10, 10, 200, 200)
    Timer(900)
    WinMove("[Class:Notepad]", "", 50, 50, 500, 500)
    If Timer(500) = 1 then Return
    Send("{Enter}Done{!}")
    If Timer(1000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("{Enter 2}try to press stop and exit{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("does not work :P{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("button does not work{!}{Enter}Func test1 is still running")
    If Timer(800) = 1 then Return
    Send(".")
    If Timer(800) = 1 then Return
    Send(".")
    If Timer(800) = 1 then Return
    Send(".")
    If Timer(1000) = 1 then Return
    Send("{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("still does not work{!}{Enter}zZzZ... 5 sec{Enter 2}")
    If Timer(5000) = 1 then Return
    WinActivate("[Class:Notepad]")
    Send("wel i wil exit now you can press stop and exit{!}")
EndFunc

Func test2()
    Sleep(2000)
EndFunc

Func Timer($iTime)

    $iBegin = TimerInit()

    While TimerDiff($iBegin) < $iTime
        If GUIGetMsg() = $Button_2 Then
            _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0)
            test2()
            Return 1
        EndIf
    WEnd

EndFunc

Of course this will only work if you have lots of idle loops in the function you want to break into. :mellow:

M23

This work good thank you
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...