SorryButImaNewbie Posted December 16, 2014 Posted December 16, 2014 (edited) Hello guys! I have searched the forum for most of today to find something I can use (I'm still sure that this question has been asked and answered, thats make posting this uncomfortable) but couldn't find what I'm looking for or If i found it i didn't understand it. When I wrote this post the first time its ended up as a very long one. I decided to shorten it I started to use https://www.autoitscript.com/wiki/Interrupting_a_running_function for start I use ISN AutoIt Studio to build my GUI, how do I check If a specific button has been clicked? _IsPressed is for keyboard buttons (also found a thread where someone made it work somehow, but since I cant really reach the GUI code...) I want to make an interrupt function thats enable the user to exit from runing functions. My plan is that i put every function in to a Do.. Until 1 Loop and make an If button has been clicked Then ExitLoop thingy, but not even sure if that would work if there is only an If loop at the start of the Do Until 1 Loop, should I "spam" my code with if ... then exitloops everywhere i can? For now I want to interrupt the test function (If I can get that work I can handle the rest) Func Test() Do GUICtrlSetOnEvent($interrupt , "_Interrupt") Then ExitLoop MsgBox(0, "Sooo programing", "Much Clever, seems so working") Sleep(500) MsgBox(0, "Test", "If you see this, it didn't work...") $fTestRun = False Until 1 EndFunc ;==>Test I tried things with GUICtrlSetOnEvent as well, but it calls a funtion, autoit doesnt allow multitasking of functions so how does that works out? Thanks for the help! Edit: sorry for the title, wanted to rewrite it and posted it insted... Edited December 16, 2014 by SorryButImaNewbie
SorryButImaNewbie Posted December 16, 2014 Author Posted December 16, 2014 ohh, the $fTestRun = false is there because I call my functions with variabels from the idle while WEnd loop and using these variables as True if it runs and false if it doesnt
andrewz Posted December 16, 2014 Posted December 16, 2014 (edited) Hmm I dont quite understand what you are trying to do... You want to interupt a function by: - Pressing a button in your GUI ? - Pressing a button on they keyboard ? - (nespresso) What else ? Please explain that a little bit more precise. EDIT: If I understand what ur trying to do, no that won't work cuz you would have to have two functions running at the same time. BUT u could make 2 seperate scripts running at the same time. EDIT2: I came up with an idea: Split the function up into two parts. 1.) MsgBox(0, "Sooo programing", "Much Clever, seems so working") 2.) MsgBox(0, "Test", "If you see this, it didn't work...") Then replace the sleep(500) in between with another function which checks if u interupt the script during this time every 0.01 seconds (100 times a second) It does that untill it "slept" for 500 miliseconds so it's equal to your sleep(500). So that would be 50 checks in the sleeptime of sleep(500), if you need more acccuricy, lower the sleep even more. That's what it looks like: expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $fInterrupt = 0 $hGUI = GUICreate("Test", 500, 500) $hButton_1 = GUICtrlCreateButton("Start", 10, 10, 80, 30) $hButton_2 = GUICtrlCreateButton("End", 10, 50, 80, 30) GUISetState() GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $hButton_1 _Func_1() EndSwitch WEnd Func _Func_1() $fInterrupt = 0 For $i = 1 To 20 MsgBox(0, "Sooo programing", "Much Clever, seems so working") _interupter(1000) MsgBox(0, "Test", "If you see this, it didn't work...") Next EndFunc Func _interupter($iDelay) $iBegin = TimerInit() Do Sleep(10) If $fInterrupt Then Exit EndIf Until TimerDiff($iBegin) > $iDelay Return 0 EndFunc Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) If BitAND($wParam, 0x0000FFFF) = $hButton_2 Then $fInterrupt = 1 Return $GUI_RUNDEFMSG EndFunc Edited December 16, 2014 by andrewz
MikahS Posted December 16, 2014 Posted December 16, 2014 Use a hotkey function to set a key to interrupt. example: HotKeySet("{ESC}", "interrupt") While 1 _Test() WEnd Func _Test() ConsoleWrite("hi") Sleep(1000) EndFunc Func interrupt() ConsoleWrite("I just got interrupted") Sleep(1000) EndFunc Hope that helps out Snips & Scripts Reveal hidden contents My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ
andrewz Posted December 16, 2014 Posted December 16, 2014 (edited) On 12/16/2014 at 3:12 PM, MikahS said: Use a hotkey function to set a key to interrupt. example: HotKeySet("{ESC}", "interrupt") While 1 _Test() WEnd Func _Test() ConsoleWrite("hi") Sleep(1000) EndFunc Func interrupt() ConsoleWrite("I just got interrupted") Sleep(1000) EndFunc Hope that helps out I don't think that's what he wanted. He wants you to be able to pause the script during the sleep. EDIT: With the button of course. @Mikah Hotkeyset ofc works. MsgBox(0, "Sooo programing", "Much Clever, seems so working") Sleep(500) ;<--- During this you should be able to pause the script by the press of a button. MsgBox(0, "Test", "If you see this, it didn't work...") Do you understand what I mean ? Edited December 16, 2014 by andrewz
BrewManNH Posted December 16, 2014 Posted December 16, 2014 To the OP, post a working example of what you're doing. As you can see by the replies no one knows what you're doing or what you're attempting to do. Posting a small snippet with obvious errors of coding as you did isn't helping. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! Reveal hidden contents I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
andrewz Posted December 16, 2014 Posted December 16, 2014 On 12/16/2014 at 3:39 PM, BrewManNH said: To the OP, post a working example of what you're doing. As you can see by the replies no one knows what you're doing or what you're attempting to do. Posting a small snippet with obvious errors of coding as you did isn't helping. It's understandable, but he wrote too much "around" it. How do I check If a specific button has been clicked? ------> I want to make an interrupt function thats enable the user to exit from runing functions. Check the script I posted, it does exactly that.
MikahS Posted December 16, 2014 Posted December 16, 2014 On 12/16/2014 at 3:33 PM, andrewz said: Do you understand what I mean ? Have a look at the HotKeySet remarks, and I just threw out an example, wasn't going to write it for them. Snips & Scripts Reveal hidden contents My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ
andrewz Posted December 16, 2014 Posted December 16, 2014 (edited) On 12/16/2014 at 4:02 PM, MikahS said: Have a look at the HotKeySet remarks, and I just threw out an example, wasn't going to write it for them. Yes you are right, it would also work with the Hotkey, but not with the button in his GUI. That's how it would look: HotKeySet("{ESC}", "interrupt") While 1 _Test() WEnd Func _Test() MsgBox(0, "Sooo programing", "Much Clever, seems so working") Sleep(500) MsgBox(0, "Test", "If you see this, it didn't work...") Sleep(1000) EndFunc Func interrupt() Exit Sleep(1000) EndFunc Edited December 16, 2014 by andrewz
MikahS Posted December 16, 2014 Posted December 16, 2014 On 12/16/2014 at 4:09 PM, andrewz said: Yes you are right, it would also work with the Hotkey, but not with the button in his GUI. Eh, yes it is possible. Have you ever used the send function before? When the button is clicked, just send the hotkey (ESC) that is assigned and it will work just the same. Snips & Scripts Reveal hidden contents My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ
Moderators Melba23 Posted December 16, 2014 Moderators Posted December 16, 2014 SorryButImaNewbie,This seems to be close to what you want: expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Opt("GUIOnEventMode", 1) Global $bStart = False $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") $cStart = GUICtrlCreateButton("Start", 10, 10, 80, 30) GUICtrlSetOnEvent($cStart, "_Start") $cStop = GUICtrlCreateButton("Stop", 10, 60, 80, 30) GUICtrlSetState($cStop, $GUI_DISABLE) GUICtrlSetOnEvent($cStop, "_Stop") GUISetState() While 1 Sleep(10) ; This is important - you MUST start the function from the main loop <<<<<<<<<<<<<< If $bStart Then _Test() EndIf WEnd Func _Start() GUICtrlSetState($cStart, $GUI_DISABLE) GUICtrlSetState($cStop, $GUI_ENABLE) $bStart = True ; Set the flag to start the function EndFunc Func _Stop() GUICtrlSetState($cStop, $GUI_DISABLE) GUICtrlSetState($cStart, $GUI_ENABLE) $bStart = False ; Clear the flag to stop the function EndFunc Func _Test() Do MsgBox($MB_SYSTEMMODAL, "Sooo programing", "Much Clever, seems so working") ; Get a timestamp $nBegin = TimerInit() ; Wait for 5 secs to see if the "Stop" button is pressed Do If $bStart = False Then ; If it was then exit the function MsgBox($MB_SYSTEMMODAL, "Interrupted", "See, it can be done") Return EndIf Until TimerDiff($nBegin) > 5000 MsgBox($MB_SYSTEMMODAL, "Test", "If you see this, it didn't work...") Until 1 EndFunc ;==>Test Func _Exit() Exit EndFuncThat is a slightly modified version of your function interruptable from a GUI button. Any use? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Reveal hidden contents ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
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