TheOnlyOne

Hotkeyset seems to stop working when i hit a while loop

15 posts in this topic

#1 ·  Posted (edited)

So currently I have some code waiting for a status to change(there is different indicators to check if this status has changed) 

Func oneRow($count)
    $globaltimer = _Timer_SetTimer($gui, 60000*10, "reset")
    $errTimer = TimerInit()
While (1)
        If PixelGetColor(1117, 326) = 0xC6C6C6 Then ExitLoop (1)
        Sleep(500)
        MouseClick("right")
        Sleep(600)
        If PixelGetColor(1117, 326) = 0xC6C6C6 Then ExitLoop (1)

        If TimerDiff($errTimer) > 5000 Then
            If PixelGetColor(1100, 310) = 0x707070 Or PixelGetColor(901, 305) = 0xE0E0E0 Or PixelGetColor(1043, 383) = 0xC6C6C6 Then
                Sleep(100)
                Send("{esc}")
                Sleep(1000)

            EndIf
        EndIf
    WEnd



....



EndFunc   ;==>oneRow

The two timers is for error checking. This works just fine the problem comes when I go into the while 1 loop, my hotkeys at the top of the program seems to stop working, for an example 

HotKeySet("{f1}", "stop")

Func stop()
    Exit
EndFunc   ;==>stop

Does not work at all after the while loop. The globaltimer is never called even though i tried setting the delay all the way down to 3 seconds. So I thought that maybe one of the function I use is blocking so the timer would never be called/the hotkey wouldn't work? if i make an delay just before the while loop the hotkey works just fine. Oh and the errTimer is supossed to not reset after 5 seconds but should keep making the check it does if five seconds have passed

Edited by TheOnlyOne
words are hard

Share this post


Link to post
Share on other sites



Can't reproduce, your code is not testable for anyone else, the rest of your code and/or the application that you're trying to work with and/or pretty much anything else may throw sand in the machine.

I tried to write a small reproducer that uses both types of timers at the same time as well and a hotkey, works fine for me:

#include <Timers.au3>

HotKeySet("x", "exiter")

doThing()
Exit

Func doThing()
    $globaltimer = _Timer_SetTimer(-1, 10000, "doBeep")
    $beepTimer = TimerInit()
    While (1)
        If TimerDiff($beepTimer) > 6000 and TimerDiff($beepTimer) < 8000 Then
            Beep(2000, 300)
        EndIf
    WEnd
EndFunc   ;==>doThing

Func doBeep()
    Beep(1000, 300)
    Sleep(100)
EndFunc   ;==>doBeep

Func exiter()
    Exit
EndFunc   ;==>exiter

Can you try and reproduce your issue with a standalone reproducer script?

1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Your script snipets should work, if the scipt is correctly completed, so post whole runable (reproducer) sdcript, showing your issue. But maybe another skript capures the F1-key?

Quote

If two AutoIt scripts set the same hotkeys, you should avoid running those scripts simultaneously as the second script cannot capture the hotkey unless the first script terminates or unregisters the key prior to the second script setting the hotkey.

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hmm not sure I can do that, I see that your example is working for me too. 

I have this code calling my function 

$return = 0
    For $i = 0 To 5
        If $pause = True Then
            pause()
        EndIf
        MsgBox(0,"","before one row")
        $return = oneRow($i)
        MsgBox(0,"",$return)
        If $return = 232 Then Return 232
        Sleep($waitDelay)
    Next

the msgboxes is something i inserted for debugging purposes. I also tried to add MsgBox(0,"","first") to the top of the oneRow function. So when i run the code it runs as expected the first time, shows "before one row" messagebox and afterwards the "first" msgbox. Then after 5 seconds it actually shows the  "before one row" msgbox again so it must have passed one of my pixelgetcolor calls(even though the status isnt correct yet). So i tried adding this to the if block that passes

If PixelGetColor(1100, 310) = 0x707070 Or PixelGetColor(901, 305) = 0xE0E0E0 Or PixelGetColor(1043, 383) = 0xC6C6C6 Then
                MsgBox(0,"","sucess " & @crlf & hex(PixelGetColor(1100, 310)) & @crlf & hex(PixelGetColor(901, 305)) & @crlf & hex(PixelGetColor(1043, 383)))
                Sleep(100)
                Send("{esc}")
                Sleep(1000)

            EndIf

But the msgbox is never shown. If i add an empty msgbox it is shown. I tried without the hex call but with the same result, is the pixegetcolor function returning errors maybe, i have never seen that before?

Another weird thing is after it shows the "before one row" msgbox the second time it doesnt show the next msgbox at the top of oneRow function ? As you said my code is properly to messy atm. I would have a hard time making a reproducable example which is not very good..

Edited by TheOnlyOne
removed duplicate code tags

Share this post


Link to post
Share on other sites

I'm not saying whether or not your code is messy, and even messy code should still work if it is actually correct (just harder to work with :)).

If you want to know how far your code gets during execution, there are a few shortcuts you could take to get some information, such as enabling the TrayIconDebug option or using Tools -> Trace: add trace lines in Scite. You can always add your own ConsoleWrite commands to show more debug information, such as results, errors and extended error information returned from function calls. (Much more useful than MsgBox'es IMHO as they don't require interaction, don't overlap any gui etc..)

I personally cannot see anything intrinsically wrong with this code (maybe someone else can?), but without clear information on when what happens exactly, it's blindfolded troubleshooting, at least to me. But that's what reproducers are for. The thing with reproducers is that if you feel like you would have a hard time making one, it's probably a sign that you have to rethink your solution to begin with, otherwise it's just trial-and-error to find a working band-aid...

1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

I'm not sure but I think "Sleep" is a blocking function.
The help file tells us: "If the current function is a "blocking" function, then the key-presses are buffered and execute as soon as the blocking function completes."


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

HotKeySet functionality remains active and also works immediately during a sleep.

/edit Tested with:

HotKeySet("x", "quitter")

Beep(1000, 200)
sleep(20000)

Func quitter()
    Beep(1500, 200)
    Exit
EndFunc   ;==>quitter

 

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Yea I might have to rethink the solution.. just annoys me because I feel like it should work. I just tried to add a tooltip to the while loop in oneRow() with the timerdiff inside. So the hotkey apperently works until 30-40 seconds have passed ?? there is no other code being executed other than the code inside the loop ..

Edited by TheOnlyOne

Share this post


Link to post
Share on other sites

Show me a simple reproducer of your script which actually works and we can check as to why you are seeing this.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Okay I might have found the problem with why the hotkeyset didn't work. I took another look at your first example @SadBunny and saw you used -1 for the hwnd. I thought you had to give it a GUI reference so i had created one like this, with the timer referencing it as shown earlier:

$gui = GUICreate("", 0, 0)

$globaltimer = _Timer_SetTimer($gui, Random(6000 * 2, 6000 * 5, 1), "reset")

I tried changing $gui to -1 and now the hotkey works even after 60+ seconds why is that? The function reset does not seem to be called though ? Tried chaning it to 10 seconds instead of a random generated number but with the same result?

Func reset()
    _Timer_KillTimer($gui, $globaltimer)
    MsgBox(0, "", "in reset")
    ...
EndFunc   ;==>reset

the callback function never seems to be called in your example nether

Edited by TheOnlyOne

Share this post


Link to post
Share on other sites

True, and I didn't want/need it to, I just wanted it to exist. Now that you mention it, the result of that _Timer_SetTimer returns 0 so the timer call fails and my test is faulty. That means that in your new case, the timer probably isn't created either and now your hotkey now remains working. So the problem still looks to be with that combination, or some contingency combined with that combination.

Finding out which one of those two is the case is a matter of elimination :) Either start from scratch with that combination and see if you can reproduce, or snip away every other contingency until you arrive at the last contingency that determines whether this problem occurs or not, and you can write a reproducer (and/or find your error :) ).

1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

So here the timer actually works :) Maybe this is better to play around with?

#include <Timers.au3>

HotKeySet("x", "exiter")

Global $gui = GUICreate("hello", 300, 300, 100, 100)
;~ GUISetState(@SW_SHOW, $gui)

doThing()
Exit

Func doThing()
    Local $globaltimer = _Timer_SetTimer($gui, 6000, "doTimerFunction")
    ConsoleWrite("Timer id: " & $globaltimer & @CRLF)
    $beepTimer = TimerInit()
    While (1)
        If TimerDiff($beepTimer) > 3000 And TimerDiff($beepTimer) < 4000 Then
            Beep(1500, 100)
        EndIf
    WEnd
    Sleep(10000)
EndFunc   ;==>doThing

Func doTimerFunction($hWnd, $iMsg, $iIDtimer, $iTime)
    Beep(1000, 300)
EndFunc   ;==>doBeep

Func exiter()
    Exit
EndFunc   ;==>exiter

 

1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Oh god i feel so stupid ..  my reset function didn't have any parameters.. Well thanks for your help, would properly been stuck with this for atleast a few hours more without it. 

Share this post


Link to post
Share on other sites

See, this is why reproducers are so important :) 9 out of 10 times (if not more) even the process of trying to come up with a reproducer from scratch, or stripping your code down until the problem disappears and then looking at the last thing you stripped, shows the actual problem.

Well, glad I could help :cheer:

1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

P.S. Small note: where you write "properly" (also earlier in this thread) you meant "probably". I'm not a grammar Nazi or anything, but confusing those two can lead to actual confusion :)

1 person likes this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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

    • 0Ethan0
      By 0Ethan0
      Ahoy Autoit Community!
      After many trials and errors I am unable to solve a problem I am facing and would appreciate any kind of input or better yet a solution 
      The Premise: An embeded slideshow viewer that runs after double-clicking an item in a ListView (each item will generate a different slideshow images).
      The Setup: GUI with a ListView Control and a simple exit button.
      The Issue: Once double clicked the slide plays however the GUI "locks"/non responsive until the slide is over. Same thing if I click on the "Test" button.
      The Culprit: I believe since it's in the images loop it can't accept any other commands until that loop is over.
      The Wish: I want to be able to use the GUI functions (selecting other items, clicking on button etc.) while the slideshow plays.
      The Code (stripped and simplified as much as I could):
      #include <GuiListView.au3> #include <File.au3> #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) HotKeySet("{Esc}", "_Exit") Global $c=0 Global $ssGDI[3], $ssGraphic[2], $ssImage Global Const $bg_color = "000000" Global Const $ssW = 480, $ssH = 320 Global $aFiles = _FileListToArrayRec("d:\testStage\", "*.jpg;*.png;*.bmp;*.gif;*.JPG;*.PNG;*.BMP;*.GIF", $FLTAR_FILES, $FLTAR_NORECUR ,$FLTAR_SORT ,$FLTAR_FULLPATH ) $guiW = 1200 $guiH = 726 $mainWindow = GUICreate("Slideshow Viewer", $guiW, $guiH, -1, -1, $WS_POPUP) $Button1 = GUICtrlCreateButton("Exit", 0, 0, 50, 50) GUICtrlSetOnEvent($Button1, "_Exit") $Button1 = GUICtrlCreateButton("Test", 60, 0, 50, 50) GUICtrlSetOnEvent($Button1, "Test") Global $ListView = GUICtrlCreateListView("Entry Name|Category", 5, 75, 195, 280) _GUICtrlListView_SetColumnWidth ($ListView, 0, 100) _GUICtrlListView_SetColumnWidth ($ListView, 1, 100) GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES) GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT) GUICtrlCreateListViewItem("Name 1|Category 1", $ListView) GUICtrlCreateListViewItem("Name 2|Category 2", $ListView) screenshotWidgetInit($ssW,$ssH, 690, 100) GUISetState(@SW_SHOW, $mainWindow) GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _Exit() EndSwitch WEnd Func Test() For $k = 1 To UBound($aFiles) - 1 screenshotWidgetTransition($aFiles[$k]) Next EndFunc Func ListView_Click() ConsoleWrite("Left Click") EndFunc Func ListView_DoubleClick() ConsoleWrite("Double Left Click") Test() EndFunc Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam) #forceref $hWndGUI, $MsgID, $wParam Local $tagNMHDR, $event, $hwndFrom, $code $tagNMHDR = DllStructCreate("int;int;int", $lParam) If @error Then Return $event = DllStructGetData($tagNMHDR, 3) Select Case $wParam = $ListView Select Case $event = $NM_CLICK ListView_Click () Case $event = $NM_DBLCLK ListView_DoubleClick () EndSelect EndSelect Return $GUI_RUNDEFMSG EndFunc Func screenshotWidgetTransition($image, $delay = 0, $speed = 1, $sleep = 2000) Local $a, $d = $c, $iX, $iY $ssImage = _GDIPlus_ImageLoadFromFile($image) $iX = _GDIPlus_ImageGetWidth($ssImage) $iY = _GDIPlus_ImageGetHeight($ssImage) $FDesktop=$ssH/$ssW $Fact =1 If $iX > $ssW And $FDesktop > ($iY/$iX) Then $Fact=$ssW/$iX ElseIf $iY > $ssH Then $Fact=$ssH/$iY EndIf $H1 = Round(($Fact * $iY),0) $W1 = Round(($Fact * $iX),0) _GDIPlus_GraphicsDrawImageRect($ssGraphic[$d], $ssImage,($ssW - $W1)/2, ($ssH - $H1) / 2,$W1,$H1) WinSetTrans($ssGDI[$d], "", 0) WinSetOnTop($ssGDI[$d], "", 1) For $a = 0 To 254 Step $speed WinSetTrans($ssGDI[$d], "", $a) Sleep($delay) Next WinSetTrans($ssGDI[$d], "", 254) WinSetOnTop($ssGDI[Not ($d)], "", 0) WinSetTrans($ssGDI[Not ($d)], "", 0) _GDIPlus_GraphicsClear($ssGraphic[Not ($d)]) $c = 1 - $d _GDIPlus_ImageDispose ($ssImage) ; very important to realease the pics Sleep($sleep) EndFunc ;==>screenshotWidgetTransition Func screenshotWidgetInit($ssW,$ssH,$ssX,$ssY) $ssGDI[2] = GUICreate("", $ssW, $ssH, $ssX, $ssY, $WS_POPUP, $WS_EX_MDICHILD, $mainWindow) $ssGDI[0] = GUICreate("", $ssW, $ssH, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $ssGDI[2]) $ssGDI[1] = GUICreate("", $ssW, $ssH, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $ssGDI[2]) ; GUISetBkColor("0x" & $bg_color, $ssGDI[2]) GUISetState(@SW_SHOW, $ssGDI[2]) GUISetState(@SW_SHOW, $ssGDI[0]) GUISetState(@SW_SHOW, $ssGDI[1]) WinSetTrans($ssGDI[0], "", 0) WinSetTrans($ssGDI[1], "", 0) _GDIPlus_Startup() $ssGraphic[0] = _GDIPlus_GraphicsCreateFromHWND($ssGDI[0]) $ssGraphic[1] = _GDIPlus_GraphicsCreateFromHWND($ssGDI[1]) _GDIPlus_GraphicsClear($ssGraphic[0], "0xFF" & $bg_color) _GDIPlus_GraphicsClear($ssGraphic[1], "0xFF" & $bg_color) EndFunc ;==>screenshotWidgetInit Func _Exit() _GDIPlus_ImageDispose($ssImage) _GDIPlus_GraphicsDispose($ssGraphic[0]) _GDIPlus_GraphicsDispose($ssGraphic[1]) GUIDelete($ssGDI[0]) GUIDelete($ssGDI[1]) GUIDelete($ssGDI[2]) _GDIPlus_Shutdown() Exit EndFunc ;==>_Exit I hope someone can shed light on this; perhaps a different approach is needed?
      Thank you in advance!
      P.S.
      The script is patched from different scripts of different users in the forum - thank you again users!
    • AndreyS
      By AndreyS
      Dear developers and creators of the language, please explain to me why when pressing the combination Crtl + Shift + C from the 5th or the 10th time "sticks / sinks" Ctrl or Shift? And then you need to press Ctrl or Shift again to reset their triggering. I only need to use hot keys like in the example!
      The code is a small example. Its essence is that when a user in the editor selects any number and presses a combination, then it increases by 1. 
      Dim $x HotKeySet("+^c","Calc") While 1 Sleep(10000) WEnd Func Calc() Send("^c") Sleep(50) $x=ClipGet()+1 ClipPut($x) Send("^v") EndFunc I thought that in my program some kind of glitch was searching for a reason for a long time. And it turned out that apparently somehow the work of some functions used in the example is not compatible. 
      Tell me, please, what is the problem?
    • Patryk
      By Patryk
      Dear Colleagues,
      I have no idea how to refer to style.display in my loop. There is a button like: 
      <input name="ctl00$bodyPlaceholder$btnFecharProcessamento" class="button" id="bodyPlaceholder_btnFecharProcessamento" style="display: none;" onclick="FecharJanelaProcessamento();return false;" type="submit" value="Fechar"> I want it to be clicked as soon as button's style change from style="display: none;" to  style= ="display: inline-block;" 
      This is what I got, but it's not working...
      Func GetButtons() $oButtons = _IEGetObjByName($oForm,"ctl00$bodyPlaceholder$btnFecharProcessamento",-1) $i = 0 While $i <> 1 For $oBtn In $oButtons If _IEFormElementGetValue($oBtn) = "Fechar" And $oBtn.document.style.display = "display: inline-block;" Then $i = 1 _IEAction($oBtn, "focus") _IEAction($oBtn, "click") ExitLoop EndIf Next WEnd EndFunc  
      I will really appreciate if you could help me with these loop!
    • Nareshm
      By Nareshm
      How to repeat my script when my control class does not match ?
       
      My Script : winActivate ( " PS :: Version - 2.2.0.0  - [PS Bill]")                   ControlClick ( "PS :: Version - 2.2.0.0  - [PS Bill]", "", "[NAME:btnSavebyFP]")                   ControlSend(" PS :: Version - 2.2.0.0  - [PS Bill]", "", "[NAME:btnSavebyFP]", "{enter}")                   WinActive ("Verification Number Entry ::")                   ControlClick("Next >", "","[NAME:btnNext]") ;if   >>>> Control <<<<  does not match repeat my script, ;if match exit script  

      Class:    WindowsForm734a
      Instance:    15
      ClassnameNN:    WindowsForm34a15
      Name:    BillPrint
      Advanced (Class):    [NAME:BillPrint]
      ID:    7875
      Text:    
      Position:    39, 310
      Size:    1221, 306
      ControlClick Coords:    745, 20
      Style:    0x56010000
      ExStyle:    0x00000000
      Handle:    0x00000000000C0468
    • SkysLastChance
      By SkysLastChance
      I am not sure why I am getting the this error on my second pass of the code.
      1 - $oWorkbook is not an object or not a workbook object
      Any help or advice on my code appreciated. 
      #include <Excel.au3> #include <Array.au3> #include <MsgBoxConstants.au3> Global $sExcelFile1 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "(*.xlsm)") Global $sExcelFile2 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "Excel Sheet (*.xlsx;*.xls)|All (*.*)") Global $vRow = 2 If FileExists($sExcelFile2) Then Global $oExcel2 = _Excel_Open () $oExcel2 = _Excel_BookOpen($oExcel2,$sExcelFile2) EndIF If FileExists($sExcelFile1) Then Global $oExcel1 = _Excel_Open () $oExcel1 = _Excel_BookOpen($oExcel1,$sExcelFile1,Default,Default,"2007") EndIF $oRead = _Excel_RangeRead ($oExcel2,"Untitled","A2",3) $oFind = _Excel_RangeFind ($oExcel1,$oRead,"E4:FD92",Default,$xlWhole) $Clip = _ArrayToClip($oFind,"",0,0,"",2,2) Send("{ScrollLock Off}") $hWnd = WinWait("[CLASS:XLMAIN]") ControlSend($hWnd, "", "", ("^g")) WinWait("[CLASS:bosa_sdm_XL9]") ; Go To ControlSend($hWnd, "", "", ("^v")) ControlSend($hWnd, "", "", ("{Enter}")) ControlSend($hWnD, "", "", "{Down " & $vRow & "}") Do $oTime = _Excel_RangeRead ($oExcel2,"Untitled","B2",3) If @error Then Exit MsgBox(0, "Error", "Error" & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox(0,"Test",$oTime) IF $oTime = "7:10:00 AM" Then $oCalls1 = _Excel_RangeRead ($oExcel2,Default,"C" & $vRow,3) $oCalls2 = _Excel_RangeRead ($oExcel2,Default,"D" & $vRow,3) ControlSend($hWnd, "", "", $oCalls1) ControlSend($hWnd, "", "", ("{RIGHT}")) ControlSend($hWnd, "", "", $oCalls2) $vRow = $vRow + 1 ContinueLoop Else $vRow = $vRow + 1 EndIf Until $vRow = 4 1.xlsm
      2.xlsx