Jump to content

Stoping a Timed Progress bar script


JotaPx
 Share

Recommended Posts

Hi,

i've been scripting around a bit with autoit but i always had this problem that i find a bit stupid on my part:

~Can't stop a script execution during a rotine...   "duh!" you may say   else take a look at this sample script:

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

$main = GUICreate("5.5 seconds!",1100,25,125,0,-1,$WS_EX_TOPMOST)

$prog = GUICtrlCreateProgress(0,0,1000,25)
$OK = GUICtrlCreateButton("GO!",1000,0,25,25)
$Stop = GUICtrlCreateButton("Stop",1025,0,50,25)
$label = GUICtrlCreateLabel("-",1075,0,25)
GUICtrlSetState($Stop,$GUI_DISABLE)
GUICtrlSetState($main,$GUI_ONTOP)
GUISetState()
$go = 1

while 1
$msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        case $msg = $OK     
            GUICtrlSetState($OK,$GUI_DISABLE)
            GUICtrlSetState($Stop,$GUI_ENABLE)
            Do
                $msg = GUIGetMsg()
                GUICtrlSetData($prog,0)
                sleep(1000) 
                GUICtrlSetData($prog,20)
                sleep(1000)
                GUICtrlSetData($prog,40)
                sleep(1000) 
                GUICtrlSetData($prog,60)
                sleep(1000) 
                GUICtrlSetData($prog,80)
                sleep(1000) 
                GUICtrlSetData($prog,100)
                sleep(250)
                MouseClick("left")
            until $msg = $GUI_EVENT_CLOSE
    EndSelect
WEnd

How can i use the Stop button to Stop the rotine? Any toughts on that?

best regards

Link to comment
Share on other sites

Hi,

There are many ways to do it, and I have chosen the Timers.

 

The GUIGetMsg function can only catch messages when it's called, in your loop you have sleep functions avoiding the msgs to be caught.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
 
Local $iPhase = 0, $iStep = 20, $iMax = 100
Local $hTimer = 0, $fGo = False
 
$main = GUICreate("5.5 seconds!", 1100, 25, 125, 0, -1, $WS_EX_TOPMOST)
 
$prog = GUICtrlCreateProgress(0, 0, 1000, 25)
$OK = GUICtrlCreateButton("GO!", 1000, 0, 25, 25)
$Stop = GUICtrlCreateButton("Stop", 1025, 0, 50, 25)
$label = GUICtrlCreateLabel("-", 1075, 0, 25)
GUICtrlSetState($Stop, $GUI_DISABLE)
GUICtrlSetState($main, $GUI_ONTOP)
GUISetState()
 
$go = 1
 
$hTimer = TimerInit()
 
While 1
    $msg = GUIGetMsg()
 
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OK
            $fGo = True
 
            GUICtrlSetState($OK, $GUI_DISABLE)
            GUICtrlSetState($Stop, $GUI_ENABLE)
 
            GUICtrlSetData($prog, 0)
        Case $Stop
            $fGo = False
 
            GUICtrlSetState($OK, $GUI_ENABLE)
            GUICtrlSetState($Stop, $GUI_DISABLE)
    EndSwitch
 
    If $fGo Then
        If $iPhase = $iMax / $iStep _ ;last step done
                And TimerDiff($hTimer) >= 250 Then
            MouseClick("left")
 
            $iPhase = 0 ;reset counter
        ElseIf TimerDiff($hTimer) >= 1000 Then
            GUICtrlSetData($prog, ($iPhase + 1) * $iStep)
 
            $iPhase += 1
 
            $hTimer = TimerInit()
        EndIf
    EndIf
WEnd
Br, FireFox. Edited by FireFox
Link to comment
Share on other sites

Hi,

There are many ways to do it, and I have chosen the Timers.

 

The GUIGetMsg function can only catch messages when it's called, in your loop you have sleep functions avoiding the msgs to be caught.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
 
Local $iPhase = 0, $iStep = 20, $iMax = 100
Local $hTimer = 0, $fGo = False
 
$main = GUICreate("5.5 seconds!", 1100, 25, 125, 0, -1, $WS_EX_TOPMOST)
 
$prog = GUICtrlCreateProgress(0, 0, 1000, 25)
$OK = GUICtrlCreateButton("GO!", 1000, 0, 25, 25)
$Stop = GUICtrlCreateButton("Stop", 1025, 0, 50, 25)
$label = GUICtrlCreateLabel("-", 1075, 0, 25)
GUICtrlSetState($Stop, $GUI_DISABLE)
GUICtrlSetState($main, $GUI_ONTOP)
GUISetState()
 
$go = 1
 
$hTimer = TimerInit()
 
While 1
    $msg = GUIGetMsg()
 
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OK
            $fGo = True
 
            GUICtrlSetState($OK, $GUI_DISABLE)
            GUICtrlSetState($Stop, $GUI_ENABLE)
 
            GUICtrlSetData($prog, 0)
        Case $Stop
            $fGo = False
 
            GUICtrlSetState($OK, $GUI_ENABLE)
            GUICtrlSetState($Stop, $GUI_DISABLE)
    EndSwitch
 
    If $fGo Then
        If $iPhase = $iMax / $iStep _ ;last step done
                And TimerDiff($hTimer) >= 250 Then
            MouseClick("left")
 
            $iPhase = 0 ;reset counter
        ElseIf TimerDiff($hTimer) >= 1000 Then
            GUICtrlSetData($prog, ($iPhase + 1) * $iStep)
 
            $iPhase += 1
 
            $hTimer = TimerInit()
        EndIf
    EndIf
WEnd
Br, FireFox.

 

 

That seamed to do the trick. Will be giving a think about it. Thanks a mil

Link to comment
Share on other sites

Just made a couple of minor changes so it resets when stop is pressed:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
 
Local $iPhase = 0, $iStep = 20, $iMax = 100
Local $hTimer = 0, $fGo = False
 
$main = GUICreate("5.5 seconds!", 1100, 25, 125, 0, -1, $WS_EX_TOPMOST)
 
$prog = GUICtrlCreateProgress(0, 0, 1000, 25)
$OK = GUICtrlCreateButton("GO!", 1000, 0, 25, 25)
$Stop = GUICtrlCreateButton("Stop", 1025, 0, 50, 25)
$label = GUICtrlCreateLabel("-", 1075, 0, 25)
GUICtrlSetState($Stop, $GUI_DISABLE)
GUICtrlSetState($main, $GUI_ONTOP)
GUISetState()
 
$hTimer = TimerInit()
 
While 1
    $msg = GUIGetMsg()
 
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $OK
            $fGo = True
 
            GUICtrlSetState($OK, $GUI_DISABLE)
            GUICtrlSetState($Stop, $GUI_ENABLE)
 
            GUICtrlSetData($prog, 0)
        Case $Stop
            $fGo = False
            $iPhase = 0
            GUICtrlSetData($prog, 0)
            GUICtrlSetState($OK, $GUI_ENABLE)
            GUICtrlSetState($Stop, $GUI_DISABLE)
    EndSwitch
 
    If $fGo Then
        If $iPhase = $iMax / $iStep _ ;last step done
                And TimerDiff($hTimer) >= 1000 Then
            GUICtrlSetData($prog, $iMax)
            MouseClick("left")
 
            $iPhase = 0 ;reset counter
        ElseIf TimerDiff($hTimer) >= 1000 Then
            GUICtrlSetData($prog, ($iPhase ) * $iStep)
 
            $iPhase += 1
 
            $hTimer = TimerInit()
        EndIf
    EndIf
WEnd

Once again ty very much for the helpfull knowledge

Edited by JotaPx
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

×
×
  • Create New...