Jump to content

Help with a toggle pause button


Recommended Posts

I'm trying to create a GUI Control button that will pause the main AutoIt routine. I want the button to be a toggle; press it once and it pauses, press it once more and it resumes again. I'm stumped as to why my toggle doesn't work. It pauses but it won't resume. I'm using version 3.1.1.121 but I also tried 3.1.1.131 beta.

Can anyone help?

#include <GUIConstants.au3>
Global $paused = 0

Opt ("GUIOnEventMode", 1)
$mainwindow = GUICreate("Hello World", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$pb = GUICtrlCreateButton("Pause", 70, 50, 60)
GUICtrlSetOnEvent($pb, "PauseButton")
GUISetState(@SW_SHOW)

While 1
  Sleep(10000) ; Idle around
WEnd

Func CLOSEClicked()
  MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
  Exit
EndFunc

Func PauseButton()
    MsgBox(0, "GUI Event", "PL1")
    $Paused = NOT $Paused
    MsgBox(0, "GUI Event", "PL2")
    While $Paused
        sleep(1000)
        MsgBox(0, "GUI Event", "PL3")
        sleep(1000)
    WEnd
    MsgBox(0, "GUI Event", "PL4")
    Sleep(500)
EndFunc
Link to comment
Share on other sites

maybe...

#include <GUIConstants.au3>
Global $paused = 0

Opt("GUIOnEventMode", 1)
$mainwindow = GUICreate("Hello World", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$pb = GUICtrlCreateButton("Pause", 70, 50, 60)
GUICtrlSetOnEvent($pb, "PauseButton")
GUISetState(@SW_SHOW)

While 1
    Sleep(10); Idle around
    
    If $paused Then
        While $paused
            ToolTip("Program is Paused", 10, 10)
            Sleep(100)
        WEnd
        ToolTip("")
    EndIf
WEnd

Func CLOSEClicked()
    If $paused Then Return
    MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
    Exit
EndFunc  ;==>CLOSEClicked

Func PauseButton()
    If $paused = 0 Then
        $paused = 1
    Else
        $paused = 0
    EndIf
EndFunc  ;==>PauseButton

8)

NEWHeader1.png

Link to comment
Share on other sites

maybe...

8)

Thanks Valuater. That definitely helped. At least it functions now. My problem is that my main routine is quite large and has some routines and functions that it does that can take 30 seconds or more. I don't want to keep interspersing the "if paused" statement. Can you see a way around this?

That's why I wanted the actual delay within the OnEvent Function.

I love the use of ToolTip rather than my Message Boxes. Thanks for that!

Link to comment
Share on other sites

  • Moderators

No one else has any ideas on how to keep the delay in the function and not in the main routine?

Is it only the completion of the event handling function that clears the event itself? Is there no other way to clear it?

Thanks,

Bennett

I'm confused on what you want, so you want to pause, but you want it to finish what it's doing first?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 2 weeks later...

I'm confused on what you want, so you want to pause, but you want it to finish what it's doing first?

No, I want it to pause immediately.

My main routine has many funtions in it, several of which last 30 seconds or more. I want to be able to immediately pause the whole program when I click on the pause button. Using the suggestion from Valuater, the Pause Button function will immediately set the variable "paused" to a value but that isn't what pauses the program. After the function has finished, it returns to the main routine and then does whatever it is doing until it hits this section:

If $paused Then
        While $paused
            ToolTip("Program is Paused", 10, 10)
            Sleep(100)
        WEnd
        ToolTip("")
    EndIf

But then I need to check that a lot in my main routine. Something like this pseudocode:

main

run routine 1 (this lasts 30 seconds)

if paused then sleep until not paused

run routine 2 (this lasts 30 seconds)

if paused then sleep until not paused

run routine 3 (this lasts 30 seconds)

if paused then sleep until not paused

run routine 4 (this lasts 30 seconds)

if paused then sleep until not paused

end main

Func PauseButton()

EndFunc ;==>PauseButton

And that would only catch it every 30 seconds but I want it to pause immediately.

Link to comment
Share on other sites

Do you mean like this:

#include <GUIConstants.au3>
Global $bPaused
### Koda GUI section start ###
$Form1 = GUICreate("AForm1", 349, 170, 335, 151)
$btnPaused = GUICtrlCreateButton("Pause", 136, 104, 75, 25, 0)
$Status1 = GUICtrlCreateLabel("Doing some stuff", 0, 152, 347, 17, $SS_SUNKEN)
$Label1 = GUICtrlCreateLabel("ALabel2", 24, 24, 299, 17)
$Progress1 = GUICtrlCreateProgress(24, 64, 305, 17)
GUISetState(@SW_SHOW)
### Koda GUI section end   ###
AdlibEnable("Paused",100)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $BtnPaused
            If $bPaused = 1 Then
                $bPaused = 0
                GuiCtrlSetData($Status1,"Ready")
            Else
                $bPaused = 1
                GuiCtrlSetData($Status1,"Paused")
                
            EndIf
    EndSwitch
    DoStuff(100)
    ;sleep(100)
WEnd

AdlibDisable()
Exit

Func DoStuff($sleepTime)
    For $x =0 to 100
        GUICtrlSetData($Progress1,$x)
        sleep($sleepTime)
    Next
EndFunc



Func Paused()
    While 1
    $nMsgPaused = GUIGetMsg()
        Switch $nMsgPaused
        Case $GUI_EVENT_CLOSE
            Exit
            Case $BtnPaused
                If $bPaused = 1 Then
                    $bPaused = 0
                    GuiCtrlSetData($Status1,"Ready")
                Else
                    $bPaused = 1
                    GuiCtrlSetData($Status1,"Paused")
                EndIf
        EndSwitch
        if $bPaused Then
            ToolTip("Paused")
            sleep(10)
        else 
            ToolTip("")
            ExitLoop
        EndIf
    WEnd
EndFunc
Link to comment
Share on other sites

I get this error message when I try to run it

(15) : ==> "Case" statement with no matching "Select" statement.:

Thanks,

BB

If your using production instead of beta then you'll probably get that error is upon compile.

If you are running beta then Is your Au3check up to date?

Anyway, here is the same script using Select instead of Switch that will work with production.

#include <GUIConstants.au3>
Global $bPaused
### Koda GUI section start ###
$Form1 = GUICreate("AForm1", 349, 170, 335, 151)
$btnPaused = GUICtrlCreateButton("Pause", 136, 104, 75, 25, 0)
$Status1 = GUICtrlCreateLabel("Ready", 0, 152, 347, 17, $SS_SUNKEN)
$Label1 = GUICtrlCreateLabel("Doing some stuff", 24, 24, 299, 17)
$Progress1 = GUICtrlCreateProgress(24, 64, 305, 17)
GUISetState(@SW_SHOW)
### Koda GUI section end   ###
AdlibEnable("Paused",100)
While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $nMsg = $BtnPaused
            If $bPaused = 1 Then
                $bPaused = 0
                GuiCtrlSetData($Status1,"Ready")
            Else
                $bPaused = 1
                GuiCtrlSetData($Status1,"Paused")
                
            EndIf
    EndSelect
    DoStuff(100)
    ;sleep(100)
WEnd

AdlibDisable()
Exit

Func DoStuff($sleepTime)
    For $x =0 to 100
        GUICtrlSetData($Progress1,$x)
        sleep($sleepTime)
    Next
EndFunc



Func Paused()
    While 1
    $nMsgPaused = GUIGetMsg()
        Select 
        Case $nMsgPaused = $GUI_EVENT_CLOSE
            Exit
        Case $nMsgPaused = $BtnPaused
            If $bPaused = 1 Then
                $bPaused = 0
                GuiCtrlSetData($Status1,"Ready")
            Else
                $bPaused = 1
                GuiCtrlSetData($Status1,"Paused")
            EndIf
        EndSelect 
        if $bPaused Then
            ToolTip("Paused")
            sleep(10)
        else 
            ToolTip("")
            ExitLoop
        EndIf
    WEnd
EndFunc

Edit: typo

Edited by eltorro
Link to comment
Share on other sites

Can I name my first born after you?

You rock. Thank you very much!

Bennett

Only if He's short and stalky. :whistle:

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