Jump to content

Cancel a loop with a button in onevent mode?


Recommended Posts

I'm trying to cancel a loop with a button, but it doesn't work....

what do i need to do to check for events from within the loop?

$btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)

Global $loopcount
Do
    If @error Then ExitLoop
    ;do some stuff
    $loopcount = $loopcount + 1
    Sleep (200)     
Until $loopcount = 50; = 10 seconds
    
    
Func Cancel_pressed()
    msgbox (48,"ERROR","Canx"   )
    $loopcount=49
EndFunc
Link to comment
Share on other sites

$btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)

Global $loopcount
Do
    If @error Then ExitLoop
   ;do some stuff
    $loopcount = $loopcount + 1
    Sleep (200)     
Until $loopcount = 50; = 10 seconds
    
    
Func Cancel_pressed()
    msgbox (48,"ERROR","Canx"   )
   ;$loopcount=49
exitloop
EndFunc

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

Func Cancel_pressed()

msgbox (48,"ERROR","Canx" )

;$loopcount=49

exitloop

EndFunc

The only thing that will do is generate an error since you cannot use ExitLoop outside a loop! Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

yes . sure

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

If you haven't already, you must define the event for the button, like this: GUICtrlSetOnEvent($btn_cancel,"Cancel_pressed")

Yep, i forgot to include that in the OP, but it's there.

Really the problem is that when in the loop the exe seems to stop polling, or listening to events from the GUI. Any ideas would be greatly appreciated...

W.

Link to comment
Share on other sites

use this, i love simple

while 1
    $msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then
        ExitLoop
    elseif  $msg=$btn_cancel Then
    msgbox (48,"ERROR","Canx"   )
    $loopcount=49
        
        
EndIf
WEnd

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 

GUICreate("GUI", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "ClickClose")

$label = GUICtrlCreateLabel("", 30, 10)
$okbutton = GUICtrlCreateButton("STOP", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "ClickStop")

GUISetState(@SW_SHOW)

;Define sentinel to break loop
Global $break = false
$count = 0

;Count forever
Do
    GuiCtrlSetData($label,$count)
    $count += 1
    Sleep(1000)
Until $break

While 1
    Sleep(1000)  ; Idle around
WEnd

Func ClickStop()
    $break = true
EndFunc

Func ClickClose()
  Exit
EndFunc

Link to comment
Share on other sites

Argh,

I still con't get it to work. Here is the stripped down version, complete witn non working cancel button:

Please help! I'm pulling my hair out on this one!

#include <GUIConstants.au3>
; Change to OnEvent mode 
Opt("GUIOnEventMode", 1)  
; Define Globals & Dim's
global $break = False
; Setup the Gui
$GUIp = GUICreate("",360,130, Default, Default, $WS_POPUP)
$res_select = GUICtrlCreateCombo("Full Screen", 30, 30, 100, 25,$CBS_DROPDOWNLIST)
    GUICtrlSetData($res_select,"640 x 480|800 x 600|1024 x 768|1280 x 1024|1600 x 1200") 
$btn_connect = GUICtrlCreateButton("Connect", 142, 30, 80, 22, 0)
    GUICtrlSetOnEvent($btn_connect, "Btn_Connect")
GuiSetState()
While 1
  Sleep(1000) ; Idle around
WEnd
;#################################################
;# Functions
;#################################################
Func Btn_Connect()
    GUICtrlSetState($res_select,$GUI_HIDE)
    GUICtrlSetState($btn_connect,$GUI_HIDE)
    $btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)
;$btn_cancel = GUICtrlCreateButton("Cancel", 142, 50, 80, 22, 0)
    GUICtrlSetOnEvent($btn_cancel, "click_canx")    
    global $prog_con = GuiCtrlCreateProgress(31, 31, 98, 18)
    global $prog_con_val
    GuiCtrlSetData($prog_con, $prog_con_val)
    
    Global $loopcount
    dim $gotping = 0
    Do
        $loopcount = $loopcount + 1
        $prog_con_val = $prog_con_val + 2
        GuiCtrlSetData($prog_con, $prog_con_val)
        Sleep (200)
    Until $break OR $loopcount = 50; = 10 seconds
    Exit
EndFunc
Func click_canx()
    MsgBox(0,"","canx clicked")
    $break = true
EndFunc
Edited by woleium
Link to comment
Share on other sites

Ok, here's the deal you will not recieve events when you're already in a function triggered by an event.

But don't worry, here's a working version (though I just did a copy and paste so this will not work too well :))

#include <GUIConstants.au3>
; Change to OnEvent mode
Opt("GUIOnEventMode", 1)
; Define Globals & Dim's
Global $break = False, $check = False
; Setup the Gui
$GUIp = GUICreate("", 360, 130, Default, Default, $WS_POPUP)

$btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)
;$btn_cancel = GUICtrlCreateButton("Cancel", 142, 50, 80, 22, 0)
GUICtrlSetOnEvent($btn_cancel, "click_canx")
GUICtrlSetState(-1, $GUI_HIDE)


$res_select = GUICtrlCreateCombo("Full Screen", 30, 30, 100, 25, $CBS_DROPDOWNLIST)
GUICtrlSetData($res_select, "640 x 480|800 x 600|1024 x 768|1280 x 1024|1600 x 1200")
$btn_connect = GUICtrlCreateButton("Connect", 142, 30, 80, 22, 0)
GUICtrlSetOnEvent($btn_connect, "Btn_Connect")
GUISetState()
While 1
    Sleep(100) ; Idle around

    If $check Then
        GUICtrlSetState($res_select, $GUI_HIDE)
        GUICtrlSetState($btn_connect, $GUI_HIDE)

        
        $btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)
        ;$btn_cancel = GUICtrlCreateButton("Cancel", 142, 50, 80, 22, 0)
        GUICtrlSetOnEvent($btn_cancel, "click_canx")
        Global $prog_con = GUICtrlCreateProgress(31, 31, 98, 18)
        Global $prog_con_val
        GUICtrlSetData($prog_con, $prog_con_val)

        Global $loopcount
        Dim $gotping = 0
        Do
            $loopcount = $loopcount + 1
            $prog_con_val = $prog_con_val + 2
            GUICtrlSetData($prog_con, $prog_con_val)
            Sleep(200)
;~      msgbox(0,"","")
        Until $break Or $loopcount = 50; = 10 seconds
        Exit
    EndIf


WEnd
;#################################################
;# Functions
;#################################################
Func Btn_Connect()
    $check = True
EndFunc   ;==>Btn_Connect


Func click_canx()
    MsgBox(0, "", "canx clicked")
    $break = True
EndFunc   ;==>click_canx
Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

I think this is a limitation of OnEvent mode. If you enter a loop inside a function, event triggers are ignored. You can verify this by trying to click the X to close the window. I had to move sentinel-controlled loops outside any functions for my Checkers game due to this.

You aren't the first to have this problem:

http://www.autoitscript.com/forum/index.ph...nside++function

Edited by weaponx
Link to comment
Share on other sites

I feel so guilty for posting the extremely dirty source above so here's a new one which is a loooot mer cleaner :)

#include <GUIConstants.au3>
; Change to OnEvent mode
Opt("GUIOnEventMode", 1)
; Define Globals & Dim's
Global $break = False, $activate = False, $active=False
Global $prog_con
Global $prog_con_val
Global  $gotping = 0
; Setup the Gui
$GUIp = GUICreate("", 360, 130, Default, Default, $WS_POPUP)

$btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)
;$btn_cancel = GUICtrlCreateButton("Cancel", 142, 50, 80, 22, 0)
GUICtrlSetOnEvent($btn_cancel, "click_canx")
GUICtrlSetState(-1, $GUI_HIDE)


$res_select = GUICtrlCreateCombo("Full Screen", 30, 30, 100, 25, $CBS_DROPDOWNLIST)
GUICtrlSetData($res_select, "640 x 480|800 x 600|1024 x 768|1280 x 1024|1600 x 1200")
$btn_connect = GUICtrlCreateButton("Connect", 142, 30, 80, 22, 0)
GUICtrlSetOnEvent($btn_connect, "Btn_Connect")
GUISetState()
While 1
    Sleep(100) ; Idle around

    If $activate Then
        $timer=TimerInit()
        GUICtrlSetState($res_select, $GUI_HIDE)
        GUICtrlSetState($btn_connect, $GUI_HIDE)
        $btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)
        GUICtrlSetOnEvent($btn_cancel, "click_canx")
        $activate=False
        $active=True
        $prog_con = GUICtrlCreateProgress(31, 31, 98, 18)
        GUICtrlSetLimit(-1,100,0)
        
    EndIf
    
    If $active Then
        GUICtrlSetData($prog_con, $prog_con_val)
            $prog_con_val = TimerDiff($timer)/50
            GUICtrlSetData($prog_con, $prog_con_val)
            If TimerDiff($timer)>5000 Or $break=True Then
                Exit
            EndIf
            
        
    EndIf


WEnd
;#################################################
;# Functions
;#################################################
Func Btn_Connect()
    $activate = True
EndFunc   ;==>Btn_Connect


Func click_canx()
    MsgBox(0, "", "canx clicked")
    $break = True
EndFunc   ;==>click_canx
Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Ok, here's the deal you will not recieve events when you're already in a function triggered by an event.

But don't worry, here's a working version (though I just did a copy and paste so this will not work too well :))

#include <GUIConstants.au3>
; Change to OnEvent mode
Opt("GUIOnEventMode", 1)
; Define Globals & Dim's
Global $break = False, $check = False
; Setup the Gui
$GUIp = GUICreate("", 360, 130, Default, Default, $WS_POPUP)

$btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)
;$btn_cancel = GUICtrlCreateButton("Cancel", 142, 50, 80, 22, 0)
GUICtrlSetOnEvent($btn_cancel, "click_canx")
GUICtrlSetState(-1, $GUI_HIDE)


$res_select = GUICtrlCreateCombo("Full Screen", 30, 30, 100, 25, $CBS_DROPDOWNLIST)
GUICtrlSetData($res_select, "640 x 480|800 x 600|1024 x 768|1280 x 1024|1600 x 1200")
$btn_connect = GUICtrlCreateButton("Connect", 142, 30, 80, 22, 0)
GUICtrlSetOnEvent($btn_connect, "Btn_Connect")
GUISetState()
While 1
    Sleep(100) ; Idle around

    If $check Then
        GUICtrlSetState($res_select, $GUI_HIDE)
        GUICtrlSetState($btn_connect, $GUI_HIDE)

        
        $btn_cancel = GUICtrlCreateButton("Cancel", 142, 30, 80, 22, 0)
        ;$btn_cancel = GUICtrlCreateButton("Cancel", 142, 50, 80, 22, 0)
        GUICtrlSetOnEvent($btn_cancel, "click_canx")
        Global $prog_con = GUICtrlCreateProgress(31, 31, 98, 18)
        Global $prog_con_val
        GUICtrlSetData($prog_con, $prog_con_val)

        Global $loopcount
        Dim $gotping = 0
        Do
            $loopcount = $loopcount + 1
            $prog_con_val = $prog_con_val + 2
            GUICtrlSetData($prog_con, $prog_con_val)
            Sleep(200)
;~      msgbox(0,"","")
        Until $break Or $loopcount = 50; = 10 seconds
        Exit
    EndIf


WEnd
;#################################################
;# Functions
;#################################################
Func Btn_Connect()
    $check = True
EndFunc   ;==>Btn_Connect


Func click_canx()
    MsgBox(0, "", "canx clicked")
    $break = True
EndFunc   ;==>click_canx

YAY!

thank you thank you thank you :)

Link to comment
Share on other sites

This solution works but unfortunately it really limits what you can do with your script. This only allows one sentinel-controlled loop in your script since it has to be outside of any function. Maybe Jos can chime in with a better idea.

Link to comment
Share on other sites

This solution works but unfortunately it really limits what you can do with your script. This only allows one sentinel-controlled loop in your script since it has to be outside of any function. Maybe Jos can chime in with a better idea.

I really don't see how it limits your scripts because you can always place you loops in the main loop and use an If statement to start them.

Or am I missing something?

Broken link? PM me and I'll send you the file!

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