Hostinator Posted December 16, 2024 Posted December 16, 2024 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 Nine Posted December 16, 2024 Solution Posted December 16, 2024 (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 December 16, 2024 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Hostinator Posted December 17, 2024 Author Posted December 17, 2024 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
Nine Posted December 17, 2024 Posted December 17, 2024 (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) : expandcollapse popup#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 December 17, 2024 by Nine typo + better code “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now