Jump to content

Help exiting a loop by clicking GUI button


Recommended Posts

I've created a GUI which has a start pushbutton which runs a process which varies in time according to the size of the given dataset. I want to give the user the option of bailing out before the process completes, so I added a stop pushbutton and defined a GUICtrlSetOnEvent for it. When I start the process, I periodically monitor the state of a variable which is set within the stop button's event handler function, to see if the stop button has been clicked or not. However, once I'm inside this function it seems that AutoIT cannot read the state of the stop button. It only starts reading it once the process is complete.

I'd be most grateful if anyone can point to what I'm doing wrong, or if this is a limitation within AutoIT.

Thanks!

Edited by MythBusted
Link to comment
Share on other sites

if you use the GUIOnEventMode you have to use it for the entire GUI, GUIGetMsg will not work with GUIOnEventMode on and a GUIOnEvent function will not work without GUIOnEventMode on.

hope that helps.

Edited by smstroble

MUHAHAHAHAHA

Link to comment
Share on other sites

The code is quite long. For the sake of not flooding this with unnecessary stuff I've only posted the related sections:

; The button declaration for the GUI

$StopButton = GUICtrlCreateButton("Cancel Data Processing", 268, 168, 195, 49, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "StopButtonclick")

; The stop button event handler

Func StopButtonclick(); Cancel post-processing
        Global $stop = 1
EndFunc

; Code snippet from the running process which I want to stop with this button

Func ProcessData()
      For $i = 0 to $dataLimit
        If GUICtrlRead($StopButton) =  Then
        $oExcel.Quit                
        ProgressOff()           
        MsgBox(48, "Warning!", "Post-processing was halted prior to completion. All required files might not have been created.")
        Return
        Else
            ; Keep going
        EndIf
.
.
.
      Next
Endfunc

As a side note, all the other GUIOnEvent functions work fine.

Thanks again!

Edited by MythBusted
Link to comment
Share on other sites

Thanks for posting the code, it was easy to see what was going wrong.

Global $stop = 0

; The button declaration for the GUI

$StopButton = GUICtrlCreateButton("Cancel Data Processing", 268, 168, 195, 49, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "StopButtonclick")

; The stop button event handler

Func StopButtonclick(); Cancel post-processing
        $stop = 1
EndFunc

; Code snippet from the running process which I want to stop with this button

Func ProcessData()
      For $i = 0 to $dataLimit
        If $stop  Then
        $oExcel.Quit                
        ProgressOff()            
        MsgBox(48, "Warning!", "Post-processing was halted prior to completion. All required files might not have been created.")
        Return
        Else
            ; Keep going
        EndIf
.
.
.
      Next
Endfunc
Link to comment
Share on other sites

Thanks for your help Manadar! However, I still get the same problem. It seems once inside the "ProcessData" function, and within that loop, $stop never gets read until after the function has completed and returned, no matter how many times I click the stop button. :whistle:

Link to comment
Share on other sites

I believe I found the problem. However, I don't know how to fix it or work around it.

It seems that while the ProcessData function is executing, no other functions may simultaneously run, nor will ProcessData stop in order to give StopButtonclick a chance to run briefly, changing the value of $stop. It seems AutoIT is a single-thread application.

I placed a message box within StopButtonclick. When ProcessData is running, nothing will happen no matter how many times I press the stop button. However, once ProcessData completes, a multitude of message boxes pop up, equal to the number of times I pressed the stop button. These boxes indeed show the correct $stop = 1 value, but it's too late - ProcessData has already completed.

Any thoughts on how I can work around this will be greatly appreciated.

Thanks again!

Link to comment
Share on other sites

You're right. AutoIt is a single-thread application, although you should be abled to do almost anything in a single-thread rather then a multi-thread. What you may be running into is a so called 'blocking function'. While the blocking function is being executed the script will pause at that line to let the function continue. Examples of blocking functions are msgbox and inputbox, some DLL calls and _IELoadWait and the likes.

Edited by Manadar
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...