Jump to content

Interrupting a long function


Go to solution Solved by Nine,

Recommended Posts

Posted

Hello,

I'm new here and have a simple question.

Why does the following script only work once?

To give you some context.

I have a function that performs many copy operations and I want to interrupt it. The script I wrote only works once. The second time I press the "Abort" button, the interrupt is ignored.
 

Thank you for your help in advance.

test code.au3

  • Solution
Posted (edited)

You CANNOT have a secondary loop running beside the main GUI loop.  (Even in onEvent mode).

You can set HotKeySet to perform actions during a long running loop.

ps. when you post code, please use the method shown in the link.  Thanks.

Edited by Nine
Posted

You are right, it seems that the programm gets stuck in the "main" function during the first OnEvent. I'm not sure thou.

Anyway, I came up with this "solution" because I don't like to give up.

#include <GUIConstantsEx.au3>
Global $h = 0

GUICreate("", 200, 100)
$bt1 = GUICtrlCreateButton("Start", 10,10,80,20)
$bt2 = GUICtrlCreateButton("Abort", 100,10,80,20)

GUISetState()
GUICtrlSetOnEvent($bt2, "abort")
main()

Func main()
    Opt("GUIOnEventMode", 0)
    $h = 0
    While 1
        Switch GUIGetMsg()
            Case $bt1
                Opt("GUIOnEventMode", 1)
                endless()
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
EndFunc
Func endless()
    While 1
        Sleep(100)
        ConsoleWrite("just to see " & Random(0,9,1) & @CRLF)
        If $h Then ExitLoop
    WEnd
    main()
EndFunc
Func abort()
    $h = 1
EndFunc

 

Posted (edited)

Interesting way to have a single GUI using both OnEvent and Non Event modes.  When you switch from Non Event to OnEvent, you GUI loop becomes the endless loop.  And when you go back to Non Event mode, your GUI loop becomes the main loop.

But the way you programmed it makes it a bit hard to read, I would suggest something like this (although the result is the same) :

#include <GUIConstantsEx.au3>

Opt("MustDeclareVars", True)

Global $h = 0

main()

Func main()
  GUICreate("", 200, 100)
  Local $bt1 = GUICtrlCreateButton("Start", 10, 10, 80, 20)
  Local $bt2 = GUICtrlCreateButton("Abort", 100, 10, 80, 20)
  GUICtrlSetOnEvent(-1, "abort")
  GUISetState()

  While 1
    Switch GUIGetMsg()
      Case $bt1
        endless()
      Case $GUI_EVENT_CLOSE
        Exit
    EndSwitch
  WEnd
EndFunc   ;==>main

Func endless()
  Opt("GUIOnEventMode", 1)
  While 1
    Sleep(10)
    ConsoleWrite("just to see " & Random(0, 9, 1) & @CRLF)
    If $h Then ExitLoop
  WEnd
  Opt("GUIOnEventMode", 0)
  $h = 0
EndFunc   ;==>endless

Func abort()
  $h = 1
EndFunc   ;==>abort

 

Edited by Nine
typo + better code

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