Grosminet

WHY a while loop speedup when the mouse is moving

4 posts in this topic

Searching inside the forum, I don't succeed to get any explanation about a strange (for me !) phenomena:

Using AutoIt, I have coded a "cortex of neurons Simulator" for which I handle several neurons activity (Action Potential and Postsynaptic Potential) which take a long time, about 20 ms per neuron to be processed. This is why I need a global loop running about 1s for a global cortex of 50 neurons.

But when I move the mouse (in any direction, like rotating above the GUI) the speed of the loop increase significantly : around x8 times more ... !

This happens only if the GUI has the focus. I did a check with the "Windows Task Manager" where I can see the CPU usage changing every time the mouse is moving (growing from 3% to 15%).

I believe to remember some lecture about the influence of the user events regarding the "Application" capability to anticipate user actions by changing it's rate of pooling events, but I'm not sure it was in an AutoIt forum, or maybe it was in an other "developer forum" (like "Code Project", "Stackoverflow" , or ...?

Could somebody help me to understand this behaviour ?

If possible, is there a programmatic method to manage the speed of a simple "For, Next" loop ? I try to change the Application priority (setting to level 4 with "ProcessSetPriority"), but this seems not very efficient ....

Thanks, Alain.


FYI: I'm using :

  • Autoit 3.3.14.2
  • Windows 7 Home Premium Service Pack 1
  • Computer ASUS N56V
  • Screen resolution: 1920x1080 (recommended)
  • Theme :Windows Classic

Share this post


Link to post
Share on other sites



You're probably using GUIGetMsg, that function has a built in sleep when nothing is going on with the GUI, when you're moving the mouse over it or actioning a control the delay is bypassed.

1 person likes this

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 Gude
How 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

Share this post


Link to post
Share on other sites

Thanks for this precision about GUIGetMsg.

The "Remarks" in the AutoIt help indicates something different, No ? : ".... This function automatically idles the CPU when required so that it can be safely used in tight loops without hogging all the CPU. ...."

--> Should it be ?  : ".... This function automatically idles the GUI polling cycle when required so that it can be safely used in tight loops without hogging all the CPU. ...."

Subsequent question: to avoid such "built in sleep" behaviour, could I use the function "GUICtrlSetOnEvent", that means using the option "GUIOnEventMode" set to 1 ?

Alain.

 

Share this post


Link to post
Share on other sites

I can confirm after recoding my work that " GUICtrlSetOnEvent" avoids the "built in sleep" behavior of the events mode "0" handling.

Thanks again BrewManNH !

Alain.

( Post can be closed )

Share this post


Link to post
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

  • Similar Content

    • SkysLastChance
      By SkysLastChance
      I keep losing the count of my $r varable when I go into the dropdown () function and call the same function.
      #include <Excel.au3> #include <AutoItConstants.au3> #include<GUIConstantsEx.au3> #include<EditConstants.au3> #include<GUIConstants.au3> Global $iBox, $oExcel, $oWorkbook, $r = 1, $x = 3 HowMany() Excel () Dropdown () Sleep (500) Func Dropdown () $list = GUICreate("Chart", 225, 80) $combobox = GUICtrlCreateCombo("Clinic 1", 10, 10, 120, 20) GUICtrlSetData(-1, "Clinic 2|Clinic 3|Clinic 4") $button = GUICtrlCreateButton("Select", 10, 40, 60, 20) GUISetState(@SW_SHOW) While 1 $guibox = GUIGetMsg() Select Case $guibox = $button Select Case GUICtrlRead($combobox) = "Clinic 1" GUIDelete($list) Auto () EndSelect Case $guibox = $GUI_EVENT_CLOSE EndSelect WEnd EndFunc Func HowMany() Local $iMsg While 1 ;~ Turn input into a number $iBox = Number(InputBox ("Regestration", "How Many Patients are there total?")) ;~ If user enters a string or cancels then get them to try again If $iBox = 0 Then $iMsg = MsgBox(1,'Regestration', 'Please enter a valid number') If $iMsg = 2 Then Exit Else ExitLoop EndIf WEnd EndFunc Func Excel() While ProcessExists("EXCEL.EXE") $ms = MsgBox(5,"","Process error. You have an Excel sheet open. You must close it in order to let this program work. Please close it now.") If $ms=2 Then Exit ;~ Doesn't require sleep since the script is paused by the MsgBox above ;~ Sleep(250) WEnd ;~ Shouldn't use Global Scope inside functions moved to top of script Local $sExcelFile = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "(*.xlsx)") If FileExists($sExcelFile) Then ;~ Shouldn't use Global Scope inside functions moved to top of script $oExcel = _Excel_Open () $oWorkbook = _Excel_BookOpen($oExcel,$sExcelFile) ;this will open the chosen xls file. Else $oExcel = _Excel_Open() $oWorkbook = _Excel_BookNew($oExcel, 2);this is here to create the xls file if it does not exist. EndIf EndFunc Func Auto() Local $aArray1 = _Excel_RangeRead($oWorkbook) ;~ If $iBox is greater than no. of rows in $aArray then $iBox equals the number of rows in $aArray If $iBox > (UBound($aArray1) - 1) Then $iBox = UBound($aArray1) - 1 For $i = 2 To UBound($aArray1) - 1 ;$i =0 Start from row A If $aArray1[$i][1] = "" Then Continueloop $sR0 = $aArray1[$i][0] ;status $sR1 = $aArray1[$i][1] ;Last Name $sR2 = $aArray1[$i][2] ;First Name $sR3 = $aArray1[$i][3] ;DOB $sR4 = $aArray1[$i][4] ;Sex $sR5 = $aArray1[$i][5] ;Mailling Address $sR6 = $aArray1[$i][6] ;Zip $sR7 = $aArray1[$i][7] ;Phone # $sR8 = $aArray1[$i][8] ;Visit Reason $sR9 = $aArray1[$i][9] ;Insurance $sR10 = $aArray1[$i][10] ;Clinic $sR11 = $aArray1[$i][11] ;Provider $sR12 = $aArray1[$i][12] ;Appt Time $sR13 = $aArray1[$i][13] ;Appt Date WinWaitActive ("Untitled - Notepad") ControlSend("Untitled - Notepad", "", "", $sR1 & ',' & $sR2 & @CR) $r += 1 If $r > $iBox Then Exit Dropdown () Next EndFunc so the second time I choose clinic 1 I want it to go to the next row which would be Champ brett in my example excel.
       
      Test.xlsx
      Example Format.xlsx
    • kevin42036
      By kevin42036
      Hi All,

      I am new to AutoIt and programming in general so any help would be greatly appreciated! 

      I am trying to create a script mainly using MouseClick and WinWait in order to automate some web processes. However, due to the lag time of the web pages and fields, sometimes my script does not run properly and I need to rerun it. I am trying to create a script that will check to see if the final line of the script was properly executed and if not, rerun the program and increase a count. Ultimately I would like some sort of report that tells me how many times it took to run to completion and total time. 

      Does anyone have any suggestions as to which functions I can use to accomplish this? I am also open to any suggestions on a better process to ensure that my program runs to completion.

      Thanks in advance!

      Kevin
    • SkysLastChance
      By SkysLastChance
      I know this code is not pretty by any means, but it will not loop. I have used this loop several diffrend times. 
      When I take out the this if statment it loops forever so I am guessing ti has to do with this.
      Any ideas on cleaning up code is appreciated too.
      If $r > $sBox Then Exit Endif #include <Excel.au3> #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> $sBox = InputBox ("Vital Site", "How Many Times") Excel () Logon () Sleep (2000) Auto () Func FormatDate($DATE) $SPLIT = StringSplit($DATE," ") $MM = StringMid($SPLIT[1],5,2) $YYYY = StringLeft($SPLIT[1],4) $DD = StringMid($SPLIT[1],7,2) Return $MM & "/" & $DD & "/" & $YYYY EndFunc Func Terminate() Exit EndFunc HotKeySet(("{ESC}"),"Terminate") Func Excel () While ProcessExists("EXCEL.EXE") $ms = MsgBox(5,"","Process error. You have an Excel sheet open. You must close it in order to let this program work. Please close it now.") If $ms=2 Then Exit Sleep(250) WEnd Global $sExcelFile = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "(*.xlsx)") If FileExists($sExcelFile) Then Global $oExcel = _Excel_Open () $oExcel = _Excel_BookOpen($oExcel,$sExcelFile) ;this will open the chosen xls file. Else $oExcel = _Excel_Open() $oWorkbook = _Excel_BookNew($oExcel, 2);this is here to create the xls file if it does not exist. EndIf EndFunc Func Logon () $portal = ShellExecute ("C:\Program Files (x86)\MEDITECH\SSDWEB.Universe\SSDWEB.LIVEF.Ring\Client.mtad") ;MediTech WinWaitActive ("[CLASS:MagicFS]", "", "MagicFS Dispaly5") Sleep (300) ControlSend("[CLASS:MagicFS]", "", "", ("{F12}")) Sleep (700) ControlSend("[CLASS:MagicFS]", "", "", ("{RIGHT}")) Sleep (700) ControlSend("[CLASS:MagicFS]", "", "", ("{DOWN}")) Sleep (700) ControlSend("[CLASS:MagicFS]", "", "", ("{RIGHT}")) Sleep (700) ControlSend("[CLASS:MagicFS]", "", "", ("{RIGHT}")) Sleep (700) ControlSend("[CLASS:MagicFS]", "", "", ("{RIGHT}")) EndFunc WinWaitActive ("[CLASS:MagicFS]", "", "MagicFS Dispaly6") Func Auto () $r = 1 Local $aArray = _Excel_RangeRead($oExcel, Default, Default,1) For $i = 1 To UBound($aArray) - 1 ;$i =0 Start from row A $sR0 = $aArray[$i][0] ;status $sR1 = $aArray[$i][1] ;first name $sR2 = $aArray[$i][2] ;Last name $sR3 = $aArray[$i][3] ;Last 4 SSN $sR4 = $aArray[$i][4] ;DOB $sR5 = $aArray[$i][5] ;Email Address $sR6 = $aArray[$i][6] ;Mailling Address $sR7 = $aArray[$i][7] ;City $sR8 = $aArray[$i][8] ;state $sR9 = $aArray[$i][9] ;Zip Code $sR10 = $aArray[$i][10] ;Gender $sR11 = $aArray[$i][11] ;Phone ControlSend("[CLASS:MagicFS]", "", "", ("{F8}")) Sleep (500) ControlSend("[CLASS:MagicFS]", "", "", ("{F8}")) Sleep (500) ControlSend("[CLASS:MagicFS]", "", "", ("N")) ControlSend("[CLASS:MagicFS]", "", "", ("{TAB}")) Sleep (3000) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR1 & @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "", ("{TAB}")) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR2 & @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "", ("{TAB}")) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR6& @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "", ("{TAB}")) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR7 & @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR8 & @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR9 & @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR11& @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR5 & @CR) ;Email7 Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR5 & @CR) ;Email Sleep (200) ControlSend("[CLASS:MagicFS]", "", "", (FormatDate($sR4) & @CR)) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR10 & @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "Edit1", $sR3 & @CR) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "", ("{F8}")) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "", ("P")) Sleep (200) $sBox = MsgBox($MB_OK & $MB_TOPMOST,"Patient Portal","After you have entered the patient push OK") Sleep (300) ControlSend("[CLASS:MagicFS]", "", "", ("{F8 3}")) Sleep (200) ControlSend("[CLASS:MagicFS]", "", "", ("{ESC}")) Sleep (200) ControlSend("[CLASS:MagicFS Modal(3)]", "", "", ("{ENTER}")) $r = $r + 1 If $r > $sBox Then Exit Endif Next EndFunc
    • rcmaehl
      By rcmaehl
      Alright, currently I'm trying to merge two loops:
       
      Local $aArray[99][13] For $iLoop = 1 to 100 $aArray[$iLoop - 1][0] = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration", $iLoop) If @error <> 0 Then ReDim $aArray[$iLoop - 1][13] ExitLoop EndIf Next For $iLoop1 = 0 to UBound($aArray, 1) - 1 For $iLoop2 = 1 to 12 $aArray[$iLoop1][$iLoop2] = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration\" & $aArray[$iLoop1][0], $iLoop2) If @error <> 0 Then ExitLoop Next Next  
      into one. Here's what I have so far:
       
      Local $aArray[99][13] For $iLoop1 = 0 to UBound($aArray, 1) - 1 For $iLoop2 = 0 to 12 ConsoleWrite("Loop1 = " & $iLoop1 & ", Loop2 = " & $iLoop2 & @CRLF) If $iLoop2 = 0 Then $aArray[$iLoop1][0] = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration", $iLoop1 + 1) If @error <> 0 Then ReDim $aArray[$iLoop1 + 1][13] ExitLoop EndIf Else $aArray[$iLoop1][$iLoop2] = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration\" & $aArray[$iLoop1][0], $iLoop2) If @error <> 0 Then ExitLoop EndIf Next Next And it keeps error-ing out trying to exceed the dimension range of the array. Does the Loop not recheck the size of the array after the original check or am I doing something else wrong.
       
      Thanks.
    • 31290
      By 31290
      Hi guys, 
      Hope you are fine today
      I'm trying to restart a function by calling it back in the script:
      Here's my code so far:
      Func f_VPN() $iInputBox = InputBox("Password", "Please Enter the User's password.") If @Error = 1 Then GUICtrlSetState($fVPN, $GUI_UNCHECKED) Else ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe") $hVPN = WinWait("Cisco AnyConnect Secure Mobility Client", "Ready to connect") ControlClick($hVPN, "", "Button1") $hVPN1 = WinWait("Cisco AnyConnect | SEE VPN", "Cancel") ControlSetText($hVPN1, "", "Edit2", @Username) Sleep(250) ControlSetText($hVPN1, "", "Edit3", $iInputBox) Sleep(250) ControlClick($hVPN1, "", "Button1") Sleep(5000) If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Login Failed" Then ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button4") WinClose($hVPN) f_VPN() Else ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe") If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Connected To SEE VPN." Then ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button1") Sleep(1000) WinClose("Cisco AnyConnect Secure Mobility Client", "") ProcessClose("vpnui.exe") ; ProcessClose("explorer.exe") <<< TO UNCOMMENT IniWrite($oIniFile, "LaptopChkBox", "VPN", "1") EndIf EndIf EndFunc I'd like to the function to be restarted in the case the password provided is not correct. 
      I've googled many things but nothing very relevant.
      Any ideas over here?
      Thanks
      -31290