Monoxide Posted January 7, 2019 Posted January 7, 2019 I have a issue that is stumping me with an array error. I have a random number picker with 2 array's, 1 array holds the numbers available, the other array holds the used numbers. It is controlled by key press set as hotkey. The error seems like a timing issue as if I have it auto run(just called the function on a loop) with a 5 sec delay it can go through 5000 random draws with no problem. When I just hold down the key or do not give it 2-3 sec between pushes it seems to error out. Error I see in the console when running, it can be after 5 numbers or could be 50 or more numbers in. Console output and Error: ! Random Number: 963 Update the Text Move # from Available to Used Delete from Available Available Numbers: 865 Used Numbers: 134 Available Numbers: 864 Used Numbers: 134 Random Numbers From : 1 to 864 Number Picked: 168 ! Random Number: 199 Update the Text Move # from Available to Used Delete from Available Update Number of Available Update Number of Used Update Number on Display Saving used array to file Update Number of Available Update Number of Used Update Number on Display Saving used array to file Update Number of Available Update Number of Used Update Number on Display Saving used array to file Update Number of Available Update Number of Used Update Number on Display Saving used array to file Update Number of Available Update Number of Used Update Number on Display Saving used array to file Delete from Available Update Number of Available Update Number of Used Update Number on Display Saving used array to file Move # from Available to Used Delete from Available Update Number of Available Update Number of Used Update Number on Display Saving used array to file Update Number of Used Update Number on Display Saving used array to file Available Numbers: 861 Used Numbers: 141 Random Numbers From : 1 to 861 Number Picked: 270 ! Random Number: 315 Update the Text Move # from Available to Used Delete from Available Update Number of Available Update Number of Used Update Number on Display Saving used array to file Update the Text Move # from Available to Used Delete from Available Update Number of Available Update Number of Used Update Number on Display Saving used array to file"C:\Program Files (x86)\AutoIt3\Include\Array.au3" (438) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: If $aArray[$iReadFrom_Index] == ChrW(0xFAB1) Then If ^ ERROR expandcollapse popup; *** Start added by AutoIt3Wrapper *** #include <FileConstants.au3> ; *** End added by AutoIt3Wrapper *** #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile_x64=..\..\..\Desktop\PillDraw.Exe #AutoIt3Wrapper_Add_Constants=n #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <Array.au3> #include <MsgBoxConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> Opt("GUIOnEventMode", 1) Dim $BagNumbers[1] = ["0"] Dim $BagNumbersTemp = 0 Dim $BagNumbersStart = 1 Dim $UsedNumbers[1] = ["0"] Dim $iMax = UBound($BagNumbers) ; get array size Dim $UsedMax = UBound($UsedNumbers) ; get array size Dim $MaxNumbers = 0 Dim $Lock = 0 Dim $sFilePathUsed = @MyDocumentsDir & "\Used.txt" Dim $sFilePathSeed = @MyDocumentsDir & "\Seed.txt" ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ConsoleWrite("Available Numbers: " & $iMax & @LF) ConsoleWrite("Used Numbers: " & $UsedMax & @LF) ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ HotKeySet("{ESC}", "Terminate") HotKeySet("`", "_RandomPicker") HotKeySet("~", "_ShowUsed") Opt("GUIOnEventMode", 1) ; Input for the Seed Number, restore previous if 0 Local $PopulatePills = InputBox("Pill Setup? ", "Please enter the total number of " & @CRLF & "Pill numbers to populate." & @CRLF & @CRLF & "Enter 0 to restore previous draw session") ConsoleWrite("Pill Input: " & $PopulatePills & @LF) If $PopulatePills = 0 Then _RestoretheSeed() Else _MakethePills($PopulatePills) _SavetheSeed($PopulatePills) EndIf ConsoleWrite("Available Numbers: " & $iMax & @LF) ConsoleWrite("Used Numbers: " & $UsedMax & @LF) $Start_Bar = ControlGetPos('', 'Start', 40965) #Region ### START Koda GUI section ### Form= ;$Form1 = GUICreate("Pill by button press", 1319, 747, 192, 124) $Form1 = GUICreate('Pill by button press', @DesktopWidth, @DesktopHeight - $Start_Bar[3] - 25) $Label1 = GUICtrlCreateLabel("Wait", 16, 0, @DesktopWidth - 16, 113, BitOR($SS_CENTER, $WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_THICKFRAME)) ;Press The Red Button GUICtrlSetFont(-1, 70, 800, 0, "Rockwell Extra Bold") GUICtrlSetBkColor(-1, 0xFF0000) $Label2 = GUICtrlCreateLabel("", 16, 150, @DesktopWidth - 16, 375, BitOR($SS_CENTER, $SS_CENTERIMAGE)) ;the number picked GUICtrlSetFont(-1, 350, 400, 0, "Rockwell Extra Bold") GUICtrlSetColor(-1, 0x000000) $Label3 = GUICtrlCreateLabel("", 16, 120, @DesktopWidth - 16, 50, BitOR($SS_CENTER, $SS_CENTERIMAGE)) ;Numbers used: Numbers available: Max Number: GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) ;$pic = GUICtrlCreatePic('C:\Users\tj\OneDrive\AutoItCode\Pill Generator\logo.bmp', 600, 130, 600, 130, BitOR($WS_EX_TRANSPARENT, $SS_CENTER, $SS_CENTERIMAGE)) $Pic1 = GUICtrlCreatePic("C:\Users\tj\OneDrive\AutoItCode\Pill Generator\logo.jpg", @DesktopWidth / 2 - 500, @DesktopHeight / 2, 1000, 200) ;, BitOR($WS_EX_TRANSPARENT, $SS_CENTER, $SS_CENTERIMAGE)) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUICtrlSetData($Label3, "Numbers used: " & $UsedMax & " Numbers left: " & $iMax & " Out of: " & $MaxNumbers) ;GUICtrlSetPos($Pic, @DesktopWidth, @DesktopHeight - 300, 600, 130) While 1 ;_RandomPicker() Sleep(250) Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idOK Exit EndSwitch WEnd Func _RandomPicker() $iMax = UBound($BagNumbers) - 1 If $iMax = 0 Then MsgBox($MB_SYSTEMMODAL, "Danger Will Robinson", "You have used all available numbers.") Else GUICtrlSetBkColor($Label1, 0xFF0000) GUICtrlSetData($Label1, "Wait for instructions") ConsoleWrite("Available Numbers: " & $iMax & @LF) ConsoleWrite("Used Numbers: " & $UsedMax & @LF) Local $NumberPicked = Random(1, $iMax, 1) ConsoleWrite("Random Numbers From : 1 to " & $iMax & @LF) ConsoleWrite("Number Picked: " & $NumberPicked & @LF) ConsoleWrite("! " & "Random Number: " & $BagNumbers[$NumberPicked] & @LF) Sleep(250) ConsoleWrite("Update the Text" & @LF) GUICtrlSetData($Label2, $BagNumbers[$NumberPicked]) ConsoleWrite("Move # from Available to Used" & @LF) _ArrayAdd($UsedNumbers, $BagNumbers[$NumberPicked]) Sleep(250) ConsoleWrite("Delete from Available" & @LF) _ArrayDelete($BagNumbers, $NumberPicked) Sleep(250) ConsoleWrite("Update Number of Available" & @LF) $iMax = UBound($BagNumbers) - 1 Sleep(250) ConsoleWrite("Update Number of Used" & @LF) $UsedMax = UBound($UsedNumbers) - 1 $Lock = 0 ConsoleWrite("Update Number on Display" & @LF) GUICtrlSetData($Label3, "Numbers used: " & $UsedMax & " Numbers left: " & $iMax & " Out of: " & $MaxNumbers) _SavetheData() EndIf $Lock = 0 EndFunc ;==>_RandomPicker Func Terminate() Exit EndFunc ;==>Terminate Func _ShowUsed() ConsoleWrite("Available Numbers: " & $iMax & @LF) ConsoleWrite("Used Numbers: " & $UsedMax & @LF) ;_ArrayDisplay($UsedNumbers, "Numbers Used") GUICtrlSetBkColor($Label1, 0x00FF00) If $Lock = 0 Then GUICtrlSetData($Label1, "Press RED button to Draw") GUICtrlSetData($Label2, "Draw") $Lock = 1 Else GUICtrlSetBkColor($Label1, 0xFF0000) GUICtrlSetData($Label1, "Wait for instructions") GUICtrlSetData($Label2, "Wait") $Lock = 0 EndIf EndFunc ;==>_ShowUsed Func _MakethePills($x) While $iMax < $x _ArrayAdd($BagNumbers, $BagNumbersStart) $BagNumbersStart = $BagNumbersStart + 1 $iMax = UBound($BagNumbers) - 1 $UsedMax = UBound($UsedNumbers) - 1 WEnd $iMax = UBound($BagNumbers) - 1 ;_ArrayDisplay($BagNumbers, "Numbers Used") $MaxNumbers = $iMax EndFunc ;==>_MakethePills Func _SavetheData() ;************************ ;save array to text file ;************************ ConsoleWrite("Saving used array to file" & @LF) _FileWriteFromArray($sFilePathUsed, $UsedNumbers, 1) ;MsgBox(0, "", "array was written to text file ") EndFunc ;==>_SavetheData Func _RestoretheData() ;************************** ;read array from text file ;************************** If Not _FileReadToArray($sFilePathUsed, $UsedNumbers) Then MsgBox(4096, "Error", " Error reading Array error:" & @error) Exit EndIf _MakethePills($PopulatePills) ;_ArrayDisplay($UsedNumbers, "Elements to deleted") ;$BagNumbers $UsedNumbers For $i = UBound($UsedNumbers) - 1 To 1 Step -1 ConsoleWrite("Searching for: " & $UsedNumbers[$i] & @CRLF) $vSrch = _ArraySearch($BagNumbers, $UsedNumbers[$i]) ;_ArraySearch($BagNumbers , $UsedNumbers[$i] , 0 , $i - 1) ConsoleWrite("VSearch:" & $vSrch & @CRLF) If $vSrch > 0 Then _ArrayDelete($BagNumbers, $vSrch) Sleep(250) Next ;_ArrayDisplay($BagNumbers, "Elements deleted") $BagNumbersStart = $BagNumbersStart + 1 $iMax = UBound($BagNumbers) - 1 $UsedMax = UBound($UsedNumbers) - 1 EndFunc ;==>_RestoretheData Func _SavetheSeed($x) ConsoleWrite("Save the Seed" & @CRLF) ConsoleWrite($x & @CRLF) _FileCreate($sFilePathSeed) If Not _FileCreate($sFilePathSeed) Then MsgBox($MB_SYSTEMMODAL, "Error", " Error Creating/Resetting log. error:" & @error) EndIf ; Open the file for writing (append to the end of a file) and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePathSeed, $FO_APPEND) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.") Return False EndIf ; Write data to the file using the handle returned by FileOpen. FileWrite($hFileOpen, $x & @CRLF) ; Close the handle returned by FileOpen. FileClose($hFileOpen) EndFunc ;==>_SavetheSeed Func _RestoretheSeed() ; Open the file for reading and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePathSeed, $FO_READ) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Return False EndIf ; Read the fist line of the file using the handle returned by FileOpen. Local $sFileRead = FileReadLine($hFileOpen, 1) $PopulatePills = $sFileRead ; Close the handle returned by FileOpen. FileClose($hFileOpen) ConsoleWrite("Restoring picked numbers" & @CRLF) _RestoretheData() EndFunc ;==>_RestoretheSeed There is also an error when compiled that I believe is the same issue. "Error: Array variable has incorrect number of subscripts or subscripts dimension range exceeded."
Nine Posted January 7, 2019 Posted January 7, 2019 In help file : "A hotkey-press *typically* interrupts the active AutoIt function/statement and runs its user function until it completes or is interrupted." If you press the same hotkey repeatedly (too rapidly), you may interrupt your own hotkey function. I would suggest you do something like : Global $Running = False Func YourHotKey () if $Running then return $Running = True ; you code here $Running = False EndFunc “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
BrewManNH Posted January 7, 2019 Posted January 7, 2019 An easier way to do it like this: Func YourHotKeyFunction() HotKeySet("`", "") ; disable the hotkey ; your code goes here HotKeySet("`", "YourHotKeyFunction") ; reenable the hotkey Endfunc This way, you disable the hotkey completely until your function has a chance to do it's thing and finish, and then when it is finished, reenable the hotkey again. 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! 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
Monoxide Posted January 8, 2019 Author Posted January 8, 2019 Quote 10 hours ago, BrewManNH said: Func YourHotKeyFunction() HotKeySet("`", "") ; disable the hotkey ; your code goes here HotKeySet("`", "YourHotKeyFunction") ; reenable the hotkeyEndfunc Thank you Nine and BrewManNH, this is exactly what fixed the issue, I didn't read far enough in the help file that the hotkey interrupts any running function. FYI HotKeySet("`") disables the hotkey just for those that view this in the future. Not whats listed in the quote
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