Jump to content

Recommended Posts

Posted

In a very simple form

#include <GUIConstants.au3>

$Form1 = GUICreate("AForm1", 145, 147, 193, 115)
$DoLoop = GUICtrlCreateButton("Do Loop", 30, 20, 75, 25, 0)
$Cancel = GUICtrlCreateButton("Cancel", 30, 60, 75, 25, 0)
$Progress1 = GUICtrlCreateProgress(35, 105, 70, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $DoLoop
            DoLoop()
    EndSwitch
WEnd

Func DoLoop()
    $bCancel = False
    Local $p =0
    While (1+1=2)
        ConsoleWrite(".")
        if GUIGetMsg() = $Cancel Then
            If MsgBox(266292,"Cancel","Cancel, Are you sure?") = 6 Then
                GuiCtrlSetData($Progress1,0)                
                ExitLoop
            EndIf
        EndIf
        GuiCtrlSetData($Progress1,$p)
        if $p <100 Then
            $p +=1
        Else
            $p =0
        EndIf
        sleep(10)
    WEnd
EndFunc

Event mode cannot be used because there is no event checking when the loop is running.

Posted (edited)

It just occured to me how to use this with event mode.

#include <GUIConstants.au3>
Opt("GUIOnEventMode",1)
$Form1 = GUICreate("AForm1", 145, 147, 193, 115)
GUISetOnEvent(-3,"ExitFunc")
$DoLoop = GUICtrlCreateButton("Do Loop", 30, 20, 75, 25, 0)
GUICtrlSetOnEvent($DoLoop,"DoLoop")
$Cancel = GUICtrlCreateButton("Cancel", 30, 60, 75, 25, 0)
$Progress1 = GUICtrlCreateProgress(35, 105, 70, 17)
GUISetState(@SW_SHOW)

While 1
    sleep(100)
WEnd

Func DoLoop()
    $bCancel = False
    Local $p =0
    Local $oldOpt = Opt("GUIOnEventMode", 0)
    While (1+1=2)
        if GUIGetMsg() = $Cancel Then
            If MsgBox(266292,"Cancel","Cancel, Are you sure?") = 6 Then
                GuiCtrlSetData($Progress1,0)                
                ExitLoop
            EndIf
        EndIf
        GuiCtrlSetData($Progress1,$p)
        if $p <100 Then
            $p +=1
        Else
            $p =0
        EndIf
        sleep(10)
    WEnd
    Opt("GUIOnEventMode",$oldOpt)
EndFunc

Func ExitFunc()
    Exit
EndFunc

Switch to message mode for the loop and then back to event mode when done.

Edited by eltorro
Posted

Yes, let's rehash the code I wrote previously because that worked so well.

Okay, you know what, here's my current code.

OK, you've got what looks like a good OnEventMode GUI script there. Since the GuiGetMsg() part is commented out, it should run.

Now what is not working at this point?

Looks to me like you must be very close at this point.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Posted

It just occured to me how to use this with event mode.

#include <GUIConstants.au3>
Opt("GUIOnEventMode",1)
$Form1 = GUICreate("AForm1", 145, 147, 193, 115)
GUISetOnEvent(-3,"ExitFunc")
$DoLoop = GUICtrlCreateButton("Do Loop", 30, 20, 75, 25, 0)
GUICtrlSetOnEvent($DoLoop,"DoLoop")
$Cancel = GUICtrlCreateButton("Cancel", 30, 60, 75, 25, 0)
$Progress1 = GUICtrlCreateProgress(35, 105, 70, 17)
GUISetState(@SW_SHOW)

While 1
    sleep(100)
WEnd

Func DoLoop()
    $bCancel = False
    Local $p =0
    Local $oldOpt = Opt("GUIOnEventMode", 0)
    While (1+1=2)
        if GUIGetMsg() = $Cancel Then
            If MsgBox(266292,"Cancel","Cancel, Are you sure?") = 6 Then
                GuiCtrlSetData($Progress1,0)                
                ExitLoop
            EndIf
        EndIf
        GuiCtrlSetData($Progress1,$p)
        if $p <100 Then
            $p +=1
        Else
            $p =0
        EndIf
        sleep(10)
    WEnd
    Opt("GUIOnEventMode",$oldOpt)
EndFunc

Func ExitFunc()
    Exit
EndFunc

Switch to message mode for the loop and then back to event mode when done.

Hey, thanks for your example!

That worked!

Well, sort of.

Turns out I was doing everything right, it just takes a HEFTY long time to go through the entire while loops, plus I had multiple while loops within that while loop.

I've placed the Cancel code at the beginning and the end of every loop, but it's still a bit slow.

But hey, it works!

Posted (edited)

Hi knight666

#include <GUIConstants.au3>

GUICreate("Template", 256, 256)

;here's some gui stuff going on, but that's not really important, is it?

While 1+1=2
$msg = GUIGetMsg()
 If $msg = $GUI_EVENT_CLOSE Then ExitLoop
 If $msg = $btnCancel Then ExitLoop
 If $msg = $btnStart2 Then Go()
Wend

GUISetState(@SW_SHOW)

Func Go()
 While 1+1=2
  ;all you really need to know is that it's MASSIVE and that this doesn't work
  If GUIGetMsg() = $btnCancel Then ExitLoop
 WEnd

Func CLOSEClicked()
  Exit
EndFunc

All you need to do is replace the line -

If GUIGetMsg() = $btnCancel Then ExitLoop

with the following two lines

GUIGetMsg()
 If Msg() = # Then ExitLoop

The # represents the button ID number for $btnCancel - which is basically the order number of controls, i.e. if you code 4 contols before the button control, then it will probably be 5 as your ID number. Use the AutoIt Window Info Tool to confirm this or some testing dialogs.

It's as simple as that ... I use it all the time to stop a routine midstream. If you have long sections of code just repeat those two lines at pertinent locations.

P.S. If you ever add an extra control in before your $btnCancel button, then you will need to increment the button ID number accordingly at that time.

:)

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Posted

Yes, each loop would have to have the lookup.

I'm glad you got it qoing.

I changed this example a little.

I put the Cancel message lookup in a function that way there is a single point to modify if necessary.

#include <GUIConstants.au3>
Opt("GUIOnEventMode",1)
$Form1 = GUICreate("AForm1", 145, 147, 193, 115)
GUISetOnEvent(-3,"ExitFunc")
$DoLoop = GUICtrlCreateButton("Do Loop", 30, 20, 75, 25, 0)
GUICtrlSetOnEvent($DoLoop,"DoLoop")
$Cancel = GUICtrlCreateButton("Cancel", 30, 60, 75, 25, 0)
$Progress1 = GUICtrlCreateProgress(35, 105, 70, 17)
GUISetState(@SW_SHOW)

While 1
    sleep(100)
WEnd

Func DoLoop()
    $bCancel = False
    Local $p =0
    Local $oldOpt = Opt("GUIOnEventMode", 0)
    While (1+1=2)
        if _CheckMsg() Then
            If MsgBox(266292,"Cancel","Cancel, Are you sure?") = 6 Then
                GuiCtrlSetData($Progress1,0)                
                ExitLoop
            EndIf
        EndIf
        GuiCtrlSetData($Progress1,$p)
        if $p <100 Then
            $p +=1
        Else
            $p =0
        EndIf
        sleep(10)
    WEnd
    Opt("GUIOnEventMode",$oldOpt)
EndFunc

Func ExitFunc()
    Exit
EndFunc

Func _CheckMsg()
    Local $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Cancel
            Return 1
        Case Else
            Return 0
    EndSwitch
EndFunc

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...