Jump to content

How to make a function "blocking" - or - How to queue up HotKeySet-requests


Go to solution Solved by Melba23,

Recommended Posts

Hi,

i have mutliple hotkeys bound to a single function (due to lack of passing arguments via HotKeySet). The function is pretty expensive, takes about 1 to 1.5 seconds.

If i hit multiple of those hotkeys in a row while the function is still running it shows a strange behaviour. I think its because of this HotkeySet-Documentation :

 

A hotkey-press *typically* interrupts the active AutoIt function/statement and runs its user function until it completes or is interrupted. Exceptions are as follows:
1) If the current function is a "blocking" function, then the key-presses are buffered and execute as soon as the blocking function completes. MsgBox() and FileSelectFolder() are examples of blocking functions.

 

Sounds like i have 2 possible solutions. Either i can "block" the function somehow, like MsgBox() does it (does the "blocking" state occur because of the expectation of an userinput?), or i have to use a semaphore in the function, which puts all additional hotkey "presses" in a sleep-loop, until the function is done, and then go on with the next.

What do you think is a good way to handle this?

Thanks in advance,

Wutklumpen

Link to comment
Share on other sites

A synchronized behaviour. No matter how often which hotkeys are pressed, process one after the other. Like 1) in the documentation:

 

*snip* key-presses are buffered and execute as soon as the blocking function completes. *snip*

Edited by Wutklumpen
Link to comment
Share on other sites

  • Moderators
  • Solution

Wutklumpen,

Welcome to the AutoIt forum. :)

I would do it something like this:

#include <Array.au3>

HotKeySet("{ESC}", "On_Exit")
HotKeySet("w", "_HotKeyPressed")
HotKeySet("e", "_HotKeyPressed")
HotKeySet("r", "_HotKeyPressed")

Global $aHotKeyCalls[1] = [0]

While 1
    Sleep(10)

    If $aHotKeyCalls[0] Then

        ; Show calling key
        ConsoleWrite($aHotKeyCalls[1] & @CRLF)

        ; Simulate function that lasts 2 secs
        Sleep(2000)

        ; Remove call
        _ArrayDelete($aHotKeyCalls, 1)
        $aHotKeyCalls[0] -= 1
    EndIf

WEnd

Func _HotKeyPressed()
    ; Add call to array
    $aHotKeyCalls[0] += 1
    ReDim $aHotKeyCalls[$aHotKeyCalls[0] + 1]
    $aHotKeyCalls[$aHotKeyCalls[0]] = @HotKeyPressed
EndFunc

Func On_Exit()
    Exit
EndFunc
Please ask if anything is unclear. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi, Melba23!

Thanks, that looks like a pretty smart solution to my problem. As well it's straight forward but i dont think i have could implemented this by myself! ;)

I wrote some comments on your code, my understanding of what it does. If you dont mind reviewing my review?^^ Could be usefull for people stumbling upon this question also.

#include <Array.au3>

HotKeySet("{ESC}", "On_Exit")
HotKeySet("w", "_HotKeyPressed")
HotKeySet("e", "_HotKeyPressed")
HotKeySet("r", "_HotKeyPressed")

Global $aHotKeyCalls[1] = [0]

While 1
    Sleep(10)

    If $aHotKeyCalls[0] Then                                       ;-- while calls in queue

        ; Show calling key
        ConsoleWrite($aHotKeyCalls[1] & @CRLF)

        ; Simulate function that lasts 2 secs
        Sleep(2000)                                            ;-- do stuff

        ; Remove call
        _ArrayDelete($aHotKeyCalls, 1)                         ;-- delete saved hotkey from queue, shrink the array by one element
        $aHotKeyCalls[0] -= 1                                  ;-- decrement queue count
    EndIf

WEnd

Func _HotKeyPressed()
    ; Add call to array
    $aHotKeyCalls[0] += 1                                          ;-- increment the current count of calls
    ReDim $aHotKeyCalls[$aHotKeyCalls[0] + 1]                      ;-- expand the call array to a size of <count of current calls> +1
    $aHotKeyCalls[$aHotKeyCalls[0]] = @HotKeyPressed               ;-- insert the hotkey-call to the recently added index (i think i can put what ever i want in here, right?)
EndFunc

Func On_Exit()
    Exit
EndFunc

 

And thank you for the warm welcome! =]

Thanks,

Wutklumpen

Link to comment
Share on other sites

  • Moderators

Wutklumpen,

Your comments are spot on. :thumbsup:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...