menu.SetIcon too slow

Discuss the future of the AutoHotkey language
quaritexa
Posts: 4
Joined: 09 Nov 2017, 21:03

menu.SetIcon too slow

28 Jan 2021, 01:26

My script creates several dozen menus with png icons. The total number of created icons is about 200. Creation of an icon (menu.SetIcon) in each menu item takes about 15 ms. As a result, the script starts in about 3 seconds. How can this process be accelerated?
User avatar
kczx3
Posts: 1255
Joined: 06 Oct 2015, 21:39

Re: menu.SetIcon too slow

28 Jan 2021, 12:01

Hmmmm. Well, I don't have that many diferent png files to use. Just using icons from a Dll doesn't seem to take nearly as long as you're reporting though. Maybe its the process of converting the png to an acceptable format to be used with a menu that takes a while. What if you populated the menu with the icons via threads using SetTimer?

Code: Select all

mainMenu := menu.new()

cb := Func("cb")

time := A_TickCount
loop (300) {
    name := "Menu #" . A_Index
    mainMenu.add(name, cb)
    mainMenu.setIcon(name, "Shell32.dll", A_Index)
}

msgbox("Started! - " (A_TickCount - time) / 1000)

mainMenu.show()

cb(*) {
    tooltip("some random callback")
}
User avatar
kczx3
Posts: 1255
Joined: 06 Oct 2015, 21:39

Re: menu.SetIcon too slow

28 Jan 2021, 12:15

This is quite crude but demonstrates my thinking using threads.

Code: Select all

global menuReady := false
mainMenu := menu.new()

cb := Func("cb")
addItem := Func("addMenuItem").bind(mainMenu, 300, cb)

SetTimer(addItem, 1)

ToolTip("Script is executing!")

#HotIf menuReady
#F10::{
    global mainMenu
    mainMenu.show()
}

cb(*) {
    tooltip("some random callback")
}

addMenuItem(gMenu, maxItems, cb) {
    global menuReady
    static currCount := 1
    
    if (currCount == maxItems) {
        SetTimer(, 0) ; we're done, mark timer for deletion
        menuReady := true
        ToolTip("Ready!")
        return
    }
    
    name := "Menu #" . currCount
    gMenu.add(name, cb)
    gMenu.setIcon(name, "Shell32.dll", currCount)
    
    currCount++
}

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 6 guests