Sign in to follow this  
Followers 0
brad25

program will not exit

11 posts in this topic

hi everyone my problem i am having is if i press start my programs starts working right now it just moves the mouse around but as long as its runing threw that code i cant exit or pause it but when it finishes i can pause and stop it heres my code maybe someone can help me

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#Include <WinAPI.au3>

Global $hGUI, $hImage, $hGraphic, $hImage1





; Create GUI

$hGUI = GUICreate("nma", 353, 160, 300, 269)
$Button1 = GUICtrlCreateButton("&Start", 270, 15, 75, 25, 0)
$Button2 = GUICtrlCreateButton("&Pause", 270, 50, 75, 25, 0)
$Button3 = GUICtrlCreateButton("&Exit", 270, 85, 75, 25, 0)


$Label2 = GUICtrlCreateLabel("Label2", 104, 56, 36, 17)
GUICtrlSetState(-1, $GUI_HIDE)

func label1_show()
$label1=GuiCtrlCreateLabel("RUNNING",280,125,50,15)
ToolTip('Script is "RUNNING"',0,0)
endfunc


func label1_show1()
$label1=GuiCtrlCreateLabel("PAUSED",280,125,50,15)
ToolTip('Script is "PAUSED"',0,0)

WinSetState("nma", "", @SW_MINIMIZE)

endfunc





;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
func hi()
    MouseClick("left", 0, 500, 2)
sleep(2000)
MouseClick("left", 399, 500, 2)
sleep(2000)
MouseClick("left", 0, 500, 2)
sleep(2000)
MouseClick("left", 399, 500, 2)
sleep(2000)
MouseClick("left", 0, 500, 2)
sleep(2000)
MouseClick("left", 399, 500, 2)
sleep(2000)
MouseClick("left", 0, 500, 2)
sleep(2000)
MouseClick("left", 399, 500, 2)
sleep(2000)
MouseClick("left", 0, 500, 2)
sleep(2000)
MouseClick("left", 399, 500, 2)
sleep(2000)
MouseClick("left", 0, 500, 2)
sleep(2000)
MouseClick("left", 399, 500, 2)
sleep(2000)
MouseClick("left", 0, 500, 2)
sleep(2000)
MouseClick("left", 399, 500, 2)
sleep(2000)
MouseClick("left", 0, 500, 2)
sleep(2000)
MouseClick("left", 399, 500, 2)
EndFunc




    



; Load PNG image
_GDIPlus_StartUp()
$hImage   = _GDIPlus_ImageLoadFromFile("D:\Documents and Settings\Brandon\Desktop\test3.png")
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUISetState()



while 1
$msg=GuiGetMsg()
If $msg=-3 Then Exit

If $msg=$button2 Then label1_show1()
If $msg=$button3 Then exit
    If $msg=$button1 Then hi()      
WEnd






; Loop until user exits
do
until GUIGetMsg() = $GUI_EVENT_CLOSE

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_ShutDown()

; Draw PNG image





Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc

Share this post


Link to post
Share on other sites



Your problem is long sleep on func hi()

try Adlib from help file to

$msg=GuiGetMsg()

If $msg=$button3 Then exit

If will probably solve your problem


TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hello, Im not sure if the adlib function will work or not. However I can tell you WHY your buttons are not working. It is because when you press the Exit button the script is currently executing the hi function. Meaning that even though $msg may equal the other buttons, it does not matter because the hi function hasn't returned yet. In the event that you cannot get the adlib function to suit your needs then perhaps you should try creating a loop within the hi function. By doing this you can check to see if a button was pressed in the interm. Also you can use the exitloop function to escape the loop. What exactly are you trying to accomplish with this automation? There are usualy better ways to automate programs such as the controlclick command.

Edited by fireryblaze

Share this post


Link to post
Share on other sites

Hello, Im not sure if the adlib function will work or not. However I can tell you WHY your buttons are not working. It is because when you press the Exit button the script is currently executing the hi function. Meaning that even though $msg may equal the other buttons, it does not matter because the hi function hasn't returned yet. In the event that you cannot get the adlib function to suit your needs then perhaps you should try creating a loop within the hi function. By doing this you can check to see if a button was pressed in the interm. Also you can use the exitloop function to escape the loop. What exactly are you trying to accomplish with this automation? There are usualy better ways to automate programs such as the controlclick command.

what im trying to do is maje the program quit when i press exit even when its reading hi()

Share this post


Link to post
Share on other sites

At the moment I can not see where this should lead :)... the Pause function should pause and not only change the label :(. Declard a global boolean $bPause = False and switch it in the WM_COMMAND on click. In the pause function add a while $bPause = True Sleep(10)...

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

Global $hGUI, $hImage, $hGraphic, $hImage1

$hGUI = GUICreate("nma", 353, 160, 300, 269)
$Button1 = GUICtrlCreateButton("&Start", 270, 15, 75, 25, 0)
$Button2 = GUICtrlCreateButton("&Pause", 270, 50, 75, 25, 0)
$Button3 = GUICtrlCreateButton("&Exit", 270, 85, 75, 25, 0)
$Label2 = GUICtrlCreateLabel("Label2", 104, 56, 36, 17)
GUICtrlSetState(-1, $GUI_HIDE)

; Load PNG image
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile("D:\Documents and Settings\Brandon\Desktop\test3.png")
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState()

While 1
    sleep(10)
WEnd

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()


Func MY_WM_PAINT($hWnd, $msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_PAINT

Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    ;$nNotifyCode = BitShift($wParam, 16)
    $nID = BitAND($wParam, 0x0000FFFF)
    ;$hCtrl = $lParam
    Switch $nID
        Case $Button1
            hi()
        Case $Button2
            label1_show1()
        Case -3, $Button3
            Exit
    EndSwitch

EndFunc

Func label1_show()
    $label1 = GUICtrlCreateLabel("RUNNING", 280, 125, 50, 15)
    ToolTip('Script is "RUNNING"', 0, 0)
EndFunc   ;==>label1_show


Func label1_show1()
    $label1 = GUICtrlCreateLabel("PAUSED", 280, 125, 50, 15)
    ToolTip('Script is "PAUSED"', 0, 0)
    WinSetState("nma", "", @SW_MINIMIZE)
EndFunc   ;==>label1_show1

Func hi()
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
EndFunc   ;==>hi

Share this post


Link to post
Share on other sites

At the moment I can not see where this should lead :)... the Pause function should pause and not only change the label :(. Declard a global boolean $bPause = False and switch it in the WM_COMMAND on click. In the pause function add a while $bPause = True Sleep(10)...

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

Global $hGUI, $hImage, $hGraphic, $hImage1

$hGUI = GUICreate("nma", 353, 160, 300, 269)
$Button1 = GUICtrlCreateButton("&Start", 270, 15, 75, 25, 0)
$Button2 = GUICtrlCreateButton("&Pause", 270, 50, 75, 25, 0)
$Button3 = GUICtrlCreateButton("&Exit", 270, 85, 75, 25, 0)
$Label2 = GUICtrlCreateLabel("Label2", 104, 56, 36, 17)
GUICtrlSetState(-1, $GUI_HIDE)

; Load PNG image
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile("D:\Documents and Settings\Brandon\Desktop\test3.png")
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState()

While 1
    sleep(10)
WEnd

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()


Func MY_WM_PAINT($hWnd, $msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_PAINT

Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    ;$nNotifyCode = BitShift($wParam, 16)
    $nID = BitAND($wParam, 0x0000FFFF)
    ;$hCtrl = $lParam
    Switch $nID
        Case $Button1
            hi()
        Case $Button2
            label1_show1()
        Case -3, $Button3
            Exit
    EndSwitch

EndFunc

Func label1_show()
    $label1 = GUICtrlCreateLabel("RUNNING", 280, 125, 50, 15)
    ToolTip('Script is "RUNNING"', 0, 0)
EndFunc   ;==>label1_show


Func label1_show1()
    $label1 = GUICtrlCreateLabel("PAUSED", 280, 125, 50, 15)
    ToolTip('Script is "PAUSED"', 0, 0)
    WinSetState("nma", "", @SW_MINIMIZE)
EndFunc   ;==>label1_show1

Func hi()
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
    Sleep(2000)
    MouseClick("left", 0, 500, 2)
    Sleep(2000)
    MouseClick("left", 399, 500, 2)
EndFunc   ;==>hi

doesnt work it still doesnt pause when clicked

Share this post


Link to post
Share on other sites

anyone else know how i can make it work??

Share this post


Link to post
Share on other sites

brad25,

Here is a version of your script which does what I think you want. Basically the code keeps checking the Exit and Pause buttons during your hi function so it can react if required.

It does this by calling 2 subfunctions during the periods where you had the Sleep(2000) lines - the first (_Sleep_Check) checks the buttons for 2 secs and then returns; the second (_Pause_Check) checks if the Pause flag has been set and, if it is, loops checking the buttons until the flag is unset.

Pressing the Pause button sets/unsets the Pause flag (what else! :) ) - which determines whether the _Pause_Check function loops or returns immediately. Pressing the Exit button starts a Return chain through the various functions where a value of 1 means "Exit now" and is acted upon when the hi function finally ends.

I have also made few other changes - mainly to enable/disable the various buttons: :(

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

Global $hGUI, $hImage, $hGraphic, $hImage1, $fPause = True, $fExit = False

; Load PNG image
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile("M:\Program\Au3 Scripts\Images\facepalm.jpg")
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

; Create GUI

$hGUI = GUICreate("nma", 353, 160, 300, 269)

$Button1 = GUICtrlCreateButton("&Start", 270, 15, 75, 25, 0)
$Button2 = GUICtrlCreateButton("&Pause", 270, 50, 75, 25, 0)
GUICtrlSetState($Button2, $GUI_DISABLE)
$Button3 = GUICtrlCreateButton("&Exit", 270, 85, 75, 25, 0)
$Label1 = GUICtrlCreateLabel("", 280, 125, 50, 15)
$Label2 = GUICtrlCreateLabel("Label2", 104, 56, 36, 17)
GUICtrlSetState(-1, $GUI_HIDE)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $Button3
            ExitLoop
        Case $Button2
            label1_show1()
        Case $Button1
            ; Disable Start button and enable pause button
            GUICtrlSetState($Button1, $GUI_DISABLE)
            GUICtrlSetState($Button2, $GUI_ENABLE)
            ; Change the labels and change the Pause flag
            label1_show()
            ; Run the function - if it returns 1 then we need to exit
            If hi() = 1 Then ExitLoop
            ; Disable the pause and enable the Start buttons
            GUICtrlSetState($Button1, $GUI_ENABLE)
            GUICtrlSetState($Button2, $GUI_DISABLE)
    EndSwitch

WEnd

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

Exit

; Functions

Func label1_show()
    $fPause = False
    GUICtrlSetData($Label1, "RUNNING")
    GUICtrlSetData($Button2, "Pause")
    ToolTip('Script is "RUNNING"', 0, 0)
EndFunc   ;==>label1_show

Func label1_show1()
    $fPause = True
    GUICtrlSetData($Label1, "PAUSED")
    GUICtrlSetData($Button2, "Resume")
    ToolTip('Script is "PAUSED"', 0, 0)

    WinSetState("nma", "", @SW_MINIMIZE)

EndFunc   ;==>label1_show1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func hi()

    ; Run the function as 8 loops
    For $i = 1 To 8
        ; Click the mouse
        MouseClick("left", 0, 500, 2)
        ; Go and wait for 2 secs - checking the pause and Exit buttons - a return value of 1 means Exit was pressed so return immediately
        If _Sleep_Check() = 1 Then Return 1
        ; Now check if Pause pressed - if so the function waits - a return value of 1 means Exit was pressed so return immediately
        If _Pause_Check() = 1 Then Return 1
        ; Repeat for the second click
        MouseClick("left", 399, 500, 2)
        If _Sleep_Check() = 1 Then Return 1
        If _Pause_Check() = 1 Then Return 1
        ; And then repeat the loop
    Next
    ; We ended the loop mormally so return 0 so we do not exit
    Return 0

EndFunc   ;==>hi

Func _Pause_Check()

    ; Check if the Pause flag is set - if it is not then we return immediately with a value of 0
    While $fPause
        ; But if it is set we keep checking on teh Exit and Resume buttons
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button3
                ; Need to exit so start the return chain
                ToolTip('Script is "EXITING"', 0, 0)
                Return 1
            Case $Button2
                ; Resume pressed, so we will exit the loop
                label1_show()
        EndSwitch
    WEnd
    ; Return the normal value
    Return 0

EndFunc   ;==>_Pause_Check

Func _Sleep_Check()

    ; Get a timestamp
    $iBegin = TimerInit()
    ; Run this loop until......
    Do
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button3
                ; Need to exit so start the return chain
                Return 1
            Case $Button2
                ; Pause pressed - setting the flag we check in the hi function
                label1_show1()
        EndSwitch
    ; Check if the delay has reached 2 secs
    Until TimerDiff($iBegin) > 2000
    ; Return the normal value
    Return 0

EndFunc   ;==>_Sleep_Check

; Draw PNG image

Func MY_WM_PAINT($hWnd, $msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_PAINT

I have commented the code liberally, so I hope that in combination with the explanation above you can follow the logic of what is happening - please ask if not. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Here is a script I made as a sample for a school project. I was looking for button clicks durring a running process and had a hard time with the program registering the clicks, so I replaced sleep in the func with a timerinit and timerdiff

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>

$fedtax=0
$gal=0
$total=0
$price=0
$tax=.06

$pump=GUICreate("Gas Pump",230,300,-1,-1)
GUICtrlCreateLabel("Gallons",10,10)
$gallon=GUICtrlCreateLabel($gal,50,10,30,24)
GUICtrlCreateLabel("Tax",80,10)
$taxes=GUICtrlCreateLabel($fedtax,110,10,30,24)
GUICtrlCreateLabel("Total",150,10)
$tot=GUICtrlCreateLabel($total,180,10,35,24)
GUICtrlCreateLabel("Price Per Gallon",50,40)
$pergal=GUICtrlCreateLabel($price,140,40,30,24)
GUICtrlCreateLabel("Sales Tax",50,60)
$stax=GUICtrlCreateLabel($tax,140,60,30,24)

$reset=GUICtrlCreateButton("Reset",150,250)

$lead=GUICtrlCreateButton("Leaded",10,90)
$unlead=GUICtrlCreateButton("Unleaded",80,90)

$gng=GUICtrlCreateButton("Gas N Go",10,120)
$fsc=GUICtrlCreateButton("Full Service",80,120)
GUICtrlSetState($gng,$GUI_DISABLE)
GUICtrlSetState($fsc,$GUI_DISABLE)

$pump=GUICtrlCreateButton("Pump",10,250)
$stop=GUICtrlCreateButton("Stop",80,250)
GUICtrlSetState($pump,$GUI_DISABLE)
GUICtrlSetState($stop,$GUI_DISABLE)


GUISetState(@SW_SHOW)

While 1
    $msg=GUIGetMsg()

    If $msg=$lead Then
        $price+=3.25
        $fedtax+=.07
        GUICtrlSetData($pergal,$price)
        GUICtrlSetData($taxes,$fedtax)
        GUICtrlSetState($lead,$GUI_DISABLE)
        GUICtrlSetState($unlead,$GUI_DISABLE)
        GUICtrlSetState($gng,$GUI_ENABLE)
        GUICtrlSetState($fsc,$GUI_ENABLE)
    ElseIf $msg=$unlead Then
        $price+=2.25
        $fedtax+=.06
        GUICtrlSetData($pergal,$price)
        GUICtrlSetData($taxes,$fedtax)
        GUICtrlSetState($lead,$GUI_DISABLE)
        GUICtrlSetState($unlead,$GUI_DISABLE)
        GUICtrlSetState($gng,$GUI_ENABLE)
        GUICtrlSetState($fsc,$GUI_ENABLE)
    ElseIf $msg=$reset Then
        $fedtax=0
        $gal=0
        $total=0
        $price=0
        GUICtrlSetData($pergal,$price)
        GUICtrlSetData($taxes,$fedtax)
        GUICtrlSetData($gallon,$gal)
        GUICtrlSetData($tot,$total)
        GUICtrlSetState($lead,$GUI_ENABLE)
        GUICtrlSetState($unlead,$GUI_ENABLE)
        GUICtrlSetState($gng,$GUI_DISABLE)
        GUICtrlSetState($fsc,$GUI_DISABLE)
        GUICtrlSetState($pump,$GUI_DISABLE)
        GUICtrlSetState($stop,$GUI_DISABLE)
    ElseIf $msg=$gng Then
        GUICtrlSetState($lead,$GUI_DISABLE)
        GUICtrlSetState($unlead,$GUI_DISABLE)
        GUICtrlSetState($gng,$GUI_DISABLE)
        GUICtrlSetState($fsc,$GUI_DISABLE)
        GUICtrlSetState($pump,$GUI_ENABLE)
        GUICtrlSetState($stop,$GUI_ENABLE)
    ElseIf $msg=$fsc Then
        $price+=1.50
        GUICtrlSetData($pergal,$price)
        GUICtrlSetState($lead,$GUI_DISABLE)
        GUICtrlSetState($unlead,$GUI_DISABLE)
        GUICtrlSetState($gng,$GUI_DISABLE)
        GUICtrlSetState($fsc,$GUI_DISABLE)
        GUICtrlSetState($pump,$GUI_ENABLE)
        GUICtrlSetState($stop,$GUI_ENABLE)
    ElseIf $msg=$pump Then
        pump()
    ElseIf $msg=$GUI_EVENT_CLOSE Then
        GUIDelete()
        Exit (1)
    EndIf
WEnd

Func pump()
    $start=TimerInit()
    While 1
        $msg=GUIGetMsg()
        If $msg=$stop Then
            ExitLoop(1)
        ElseIf $msg=$GUI_EVENT_CLOSE Then
            GUIDelete()
            Exit (1)
        EndIf
        $diff=TimerDiff($start)
        If $diff>1000 Then
            $start=TimerInit()
            $gal+=1
            $total1=$gal*$price
            $total2=$total1*$fedtax
            $total3=$total1*$tax
            $total=$total2+$total3+$total1
            $total4=Round($total,2)
            GUICtrlSetData($gallon,$gal)
            GUICtrlSetData($pergal,$price)
            GUICtrlSetData($tot,$total4)
        EndIf
    WEnd
EndFunc

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

brad25,

Here is a version of your script which does what I think you want. Basically the code keeps checking the Exit and Pause buttons during your hi function so it can react if required.

It does this by calling 2 subfunctions during the periods where you had the Sleep(2000) lines - the first (_Sleep_Check) checks the buttons for 2 secs and then returns; the second (_Pause_Check) checks if the Pause flag has been set and, if it is, loops checking the buttons until the flag is unset.

Pressing the Pause button sets/unsets the Pause flag (what else! ;) ) - which determines whether the _Pause_Check function loops or returns immediately. Pressing the Exit button starts a Return chain through the various functions where a value of 1 means "Exit now" and is acted upon when the hi function finally ends.

I have also made few other changes - mainly to enable/disable the various buttons: :(

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

Global $hGUI, $hImage, $hGraphic, $hImage1, $fPause = True, $fExit = False

; Load PNG image
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile("M:\Program\Au3 Scripts\Images\facepalm.jpg")
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

; Create GUI

$hGUI = GUICreate("nma", 353, 160, 300, 269)

$Button1 = GUICtrlCreateButton("&Start", 270, 15, 75, 25, 0)
$Button2 = GUICtrlCreateButton("&Pause", 270, 50, 75, 25, 0)
GUICtrlSetState($Button2, $GUI_DISABLE)
$Button3 = GUICtrlCreateButton("&Exit", 270, 85, 75, 25, 0)
$Label1 = GUICtrlCreateLabel("", 280, 125, 50, 15)
$Label2 = GUICtrlCreateLabel("Label2", 104, 56, 36, 17)
GUICtrlSetState(-1, $GUI_HIDE)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $Button3
            ExitLoop
        Case $Button2
            label1_show1()
        Case $Button1
            ; Disable Start button and enable pause button
            GUICtrlSetState($Button1, $GUI_DISABLE)
            GUICtrlSetState($Button2, $GUI_ENABLE)
            ; Change the labels and change the Pause flag
            label1_show()
            ; Run the function - if it returns 1 then we need to exit
            If hi() = 1 Then ExitLoop
            ; Disable the pause and enable the Start buttons
            GUICtrlSetState($Button1, $GUI_ENABLE)
            GUICtrlSetState($Button2, $GUI_DISABLE)
    EndSwitch

WEnd

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

Exit

; Functions

Func label1_show()
    $fPause = False
    GUICtrlSetData($Label1, "RUNNING")
    GUICtrlSetData($Button2, "Pause")
    ToolTip('Script is "RUNNING"', 0, 0)
EndFunc   ;==>label1_show

Func label1_show1()
    $fPause = True
    GUICtrlSetData($Label1, "PAUSED")
    GUICtrlSetData($Button2, "Resume")
    ToolTip('Script is "PAUSED"', 0, 0)

    WinSetState("nma", "", @SW_MINIMIZE)

EndFunc   ;==>label1_show1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func hi()

    ; Run the function as 8 loops
    For $i = 1 To 8
        ; Click the mouse
        MouseClick("left", 0, 500, 2)
        ; Go and wait for 2 secs - checking the pause and Exit buttons - a return value of 1 means Exit was pressed so return immediately
        If _Sleep_Check() = 1 Then Return 1
        ; Now check if Pause pressed - if so the function waits - a return value of 1 means Exit was pressed so return immediately
        If _Pause_Check() = 1 Then Return 1
        ; Repeat for the second click
        MouseClick("left", 399, 500, 2)
        If _Sleep_Check() = 1 Then Return 1
        If _Pause_Check() = 1 Then Return 1
        ; And then repeat the loop
    Next
    ; We ended the loop mormally so return 0 so we do not exit
    Return 0

EndFunc   ;==>hi

Func _Pause_Check()

    ; Check if the Pause flag is set - if it is not then we return immediately with a value of 0
    While $fPause
        ; But if it is set we keep checking on teh Exit and Resume buttons
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button3
                ; Need to exit so start the return chain
                ToolTip('Script is "EXITING"', 0, 0)
                Return 1
            Case $Button2
                ; Resume pressed, so we will exit the loop
                label1_show()
        EndSwitch
    WEnd
    ; Return the normal value
    Return 0

EndFunc   ;==>_Pause_Check

Func _Sleep_Check()

    ; Get a timestamp
    $iBegin = TimerInit()
    ; Run this loop until......
    Do
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button3
                ; Need to exit so start the return chain
                Return 1
            Case $Button2
                ; Pause pressed - setting the flag we check in the hi function
                label1_show1()
        EndSwitch
    ; Check if the delay has reached 2 secs
    Until TimerDiff($iBegin) > 2000
    ; Return the normal value
    Return 0

EndFunc   ;==>_Sleep_Check

; Draw PNG image

Func MY_WM_PAINT($hWnd, $msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_PAINT

I have commented the code liberally, so I hope that in combination with the explanation above you can follow the logic of what is happening - please ask if not. :)

M23

that works perfectly you are awesome :)

Share this post


Link to post
Share on other sites

How I do this with OnEventMode, and how I break loop with GUI but not exit?

thx

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $hGUI, $fPause = True, $fExit = False


; Create GUI

$hGUI = GUICreate("nma", 353, 160, 300, 269)

$Button1 = GUICtrlCreateButton("&Start", 270, 15, 75, 25, 0)
$Button2 = GUICtrlCreateButton("&Pause", 270, 50, 75, 25, 0)
GUICtrlSetState($Button2, $GUI_DISABLE)
$Button3 = GUICtrlCreateButton("&Exit", 270, 85, 75, 25, 0)
$Label1 = GUICtrlCreateLabel("", 280, 125, 50, 15)
$Label2 = GUICtrlCreateLabel("Label2", 104, 56, 36, 17)
GUICtrlSetState(-1, $GUI_HIDE)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $Button3
            ExitLoop
        Case $Button2
            label1_show1()
        Case $Button1
            ; Disable Start button and enable pause button
            GUICtrlSetState($Button1, $GUI_DISABLE)
            GUICtrlSetState($Button2, $GUI_ENABLE)
            ; Change the labels and change the Pause flag
            label1_show()
            ; Run the function - if it returns 1 then we need to exit
            If hi() = 1 Then ExitLoop
            ; Disable the pause and enable the Start buttons
            GUICtrlSetState($Button1, $GUI_ENABLE)
            GUICtrlSetState($Button2, $GUI_DISABLE)
    EndSwitch
WEnd

Exit

; Functions
Func label1_show()
    $fPause = False
    GUICtrlSetData($Label1, "RUNNING")
    GUICtrlSetData($Button2, "Pause")
    ToolTip('Script is "RUNNING"', 0, 0)
EndFunc   ;==>label1_show

Func label1_show1()
    $fPause = True
    GUICtrlSetData($Label1, "PAUSED")
    GUICtrlSetData($Button2, "Resume")
    ToolTip('Script is "PAUSED"', 0, 0)
    ;WinSetState("nma", "", @SW_MINIMIZE)
EndFunc   ;==>label1_show1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func hi()
    $i=0;
    while $i<100
        If _Sleep_Check() = 1 Then Return 1
        If _Pause_Check() = 1 Then Return 1
        sleep(200)
        ConsoleWrite("var=" & $i & @CRLF)
        $i=$i+1
    wend
    ; We ended the loop mormally so return 0 so we do not exit
    Return 0
EndFunc   ;==>hi

Func _Pause_Check()
    ; Check if the Pause flag is set - if it is not then we return immediately with a value of 0
    While $fPause
        ; But if it is set we keep checking on teh Exit and Resume buttons
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button3
                ; Need to exit so start the return chain
                ToolTip('Script is "EXITING"', 0, 0)
                Return 1
            Case $Button2
                ; Resume pressed, so we will exit the loop
                label1_show()
        EndSwitch
    WEnd
    ; Return the normal value
    Return 0

EndFunc   ;==>_Pause_Check

Func _Sleep_Check()
    ; Get a timestamp
    $iBegin = TimerInit()
    ; Run this loop until......
    Do
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button3
                ; Need to exit so start the return chain
                Return 1
            Case $Button2
                ; Pause pressed - setting the flag we check in the hi function
                label1_show1()
        EndSwitch
    ; Check if the delay has reached 2 secs
    Until TimerDiff($iBegin) > 10
    ; Return the normal value
    Return 0

EndFunc   ;==>_Sleep_Check

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