Best way to ensure single instance of GUI?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kunkel321
Posts: 1062
Joined: 30 Nov 2015, 21:19

Best way to ensure single instance of GUI?

15 Apr 2024, 10:06

With some help from gpt, then some additional tweaking, I got the below code working. Is this the best way to ensure that I don't get multiple instances of g2? (Assume that the user clicks the g1 'make' button over and over.)

It occurs to me to keep g2 in RAM and just show/hide it... Should I do that, or do you guys think the approach below is all-good?

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; multi gui test

g1 := Gui()
g1.add('text',, 'click for on-demand gui')
g1.add('button', 'w300', 'make').OnEvent('click', makeg2)
g1.Show()

g2 := 0  ; Initialize g2 as null

makeg2(*)
{	; Check if g2 exists and is an object, then destroy it
	global g2
	if (IsObject(g2))
		g2.Destroy()
	g2 := Gui()
	g2.add('text',, 'this is gui 2')
	g2butt := g2.add('button', 'w200', 'okay')
	g2butt.OnEvent('click', alldone)
	g2.Show()
}

alldone(*)
{	g2.Destroy() 
	MsgBox('thank you')
}
ste(phen|ve) kunkel
RussF
Posts: 1270
Joined: 05 Aug 2021, 06:36

Re: Best way to ensure single instance of GUI?

15 Apr 2024, 10:22

I always create all my GUIs up front in the autoexec section and then just show or hide them as need be. I've seen many others here agree with that. It's not like you're talking about gigabytes of RAM being used to keep your GUIs loaded - you'll actually probably hardly notice a difference.

Russ
User avatar
Seven0528
Posts: 347
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Best way to ensure single instance of GUI?

15 Apr 2024, 10:58

 Personally, if it isn't too heavy, I prefer to Destroy and recreate Gui every time.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
gui1 := gui()
gui1.add("Button", "w300", "Gui1").onEvent("Click", gui1Button_Click)
gui1.Show()
gui1Button_Click(guiCtrlObj, info)    {
    gui2.show()
}
;---------------------
gui2 := gui()
gui2.add("Button", "w200", "Gui2").onEvent("Click", gui2Button_Click)
gui2.onEvent("Close", gui2_Close)
gui2Button_Click(guiCtrlObj, info)    {
    winClose(guiCtrlObj.gui)
}
gui2_Close(guiObj)    {
    guiObj.hide()
    return true ;  Prevent the window from closing.
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
xMaxrayx
Posts: 168
Joined: 06 Dec 2022, 02:56
Contact:

Re: Best way to ensure single instance of GUI?

15 Apr 2024, 11:34

@kunkel321
depends if you use g2 a lot you better keep it on RAM by hide() method, RAM is cheap these days and people prefer speed and snapy experience.

you can add if statement to check if g2 exited like this (i didn't test it you gone need to fix it.)

if winexit(your g2 title){
g2.show()
return
}
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
Noitalommi_2
Posts: 225
Joined: 16 Aug 2023, 10:58

Re: Best way to ensure single instance of GUI?

15 Apr 2024, 20:26

Hi.

You could also disable the button to prevent it from being used multiple times.

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+

g1 := Gui()
g1.add('text',, 'click for on-demand gui')
g1.add('button', 'w300', 'make').OnEvent('click', makeg2)
g1.Show()

makeg2(GuiCtrlObj, *) {

	GuiCtrlObj.Enabled := false

	g2 := Gui()
	g2.add('text',, 'this is gui 2')
	g2butt := g2.add('button', 'w200', 'okay')
	g2butt.OnEvent('click', alldone)
	g2.Show()

	alldone(*) {
		g2.Destroy()
		MsgBox('thank you')
		GuiCtrlObj.Enabled := true
	}
}
User avatar
kunkel321
Posts: 1062
Joined: 30 Nov 2015, 21:19

Re: Best way to ensure single instance of GUI?

15 Apr 2024, 20:48

These are all excellent approaches. Thanks Folks!
ste(phen|ve) kunkel

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, jaccotjuhhh, lukepker, sanmaodo and 68 guests