Jump to content

Gui OnEvent: Start/Stop/Pause


flet
 Share

Recommended Posts

Dear AutoIt community,

I want to simulate the behaviour of the Start/Stop/Pause buttons in Winamp or any other Media Player, but I'm stuck with problem. Please consider my code below. Start, Stop and Pause all seem to function reasonably well. However, my long function can only be paused after each time $i is updated in the for-loop, and I want the program to be able to pause between all actions in that for-loop. Is this possible (without using Hotkeys or accelerator, and without using "If $pause_pressed Then" after each action)? What is the best practice here? I don't think my method is very efficient

Thank you in advance,

#include <GUIConstantsEx.au3>
 
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
 
; Global variables
Global $start_pressed = 0
Global $stop_pressed = 0
Global $pause_pressed = 0
Global $paused = 0
 
; GUI
Global $gui_main = GUICreate("Start/Stop/Pause", 230, 120)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
Global $label = GUICtrlCreateLabel("0", 10, 10, 250, 16)
Global $button_start = GUICtrlCreateButton("&Start", 10, 31, 100)
GUICtrlSetOnEvent($button_start, "_ButtonStart")
Global $button_stop = GUICtrlCreateButtoN("S&top", 120, 31, 100)
GUICtrlSetOnEvent($button_stop, "_ButtonStop")
Global $button_pause = GUICtrlCreateButton("Pa&use", 10, 61, 100)
GUICtrlSetOnEvent($button_pause, "_ButtonPause")
GUISetState(@SW_SHOW, $gui_main)
 
While 1
    If $start_pressed Then
        $start_pressed = 0
        ReallyLongFunc()
    EndIf
    Sleep(10)
WEnd
 
; Control Events
Func _Exit()
    Exit
EndFunc
 
Func _ButtonStart()
    $start_pressed = 1
    ConsoleWrite("Start pressed" & @CRLF)
EndFunc
 
Func _ButtonStop()
   $stop_pressed = 1
   ConsoleWrite("Stop pressed" & @CRLF)
EndFunc
 
Func _ButtonPause()
   $pause_pressed = 1
   ConsoleWrite("Pause pressed" & @CRLF)
EndFunc
 
; Rest of the functions: ReallyLongFunc has to be executed repeatedly until user is fed up with it and presses stop-button
Func ReallyLongFunc()
While 1
  For $i = 1 To 50
   If $start_pressed Then
    $start_pressed = 0
    $stop_pressed = 0
    $pause_pressed = 0
    $i = 1
   ElseIf $pause_pressed Then
    $pause_pressed = 0
    Pause()
   ElseIf $stop_pressed Then
    $stop_pressed = 0
    ExitLoop 2
   EndIf
   GuiCtrlSetData($label, $i)
   ; lot of actions, here represented by these Sleep(100) statements.
   ; I'm wondering whether program can be paused between actions
   ; without checking "if ($pause_pressed)" after each line in this for-loop?
   Sleep(100)
   Sleep(100)
   Sleep(100)
   Sleep(100)
  Next
WEnd
EndFunc
 
Func Pause()
$paused = Not $paused
If $paused Then
  GUICtrlSetData($button_pause, "Res&ume")
Else
  GUICtrlSetData($button_pause, "Pa&use")
EndIf
While $paused
  If $pause_pressed Then ExitLoop
  Sleep(100)
WEnd
EndFunc
Link to comment
Share on other sites

Sleep is not a good way to simulate that, because it will pause the script while it's counting down. This might help you find a way to interrupt the function.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I've already read the wiki, but it's not clear to me how I can pause my ReallyLongFunc in an efficient way (without hotkeys or accelerators: button only). I am already able to interrupt the function by pressing the Stop-button or closing the gui.

The sleep(100) statements was just for demonstrating purposes to keep things simple in this code snippet. It could have been other code, like logging in, writing to a file or various other things...

What's the best way to pause? Checking after each line whether or not Pause has been pressed doesn't seem efficient to me. I need a few more pointers in the right direction, if you please

Link to comment
Share on other sites

The only way you'll achieve the results you want is to find places in your code that are good points to get to before you check to see if it needs to be paused/stopped/restarted. If you're scanning a 1000 files for a text string in them, then a good place would be just after the For or the While in a loop function, for example.

The sleep(100) statements was just for demonstrating purposes to keep things simple in this code snippet. It could have been other code, like logging in, writing to a file or various other things...

I know why you put them in there, I was merely telling you that it was a poor way of simulating a large section of code because nothing will interrupt the sleep function.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 7 months later...

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