Gui restore if its already running and hidden rather than starting a second instance and force closing Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Gui restore if its already running and hidden rather than starting a second instance and force closing

22 Mar 2022, 12:40

Currently I am force closing if the instance is already running
what i am trying to do is instead of force closing and starting the new instance , just winrestore old instance gui
any idea ?

Code: Select all

#SingleInstance off ;cant use single instance force because it will show couldnt close previous instance message

KillOtherInstances()  ; this is what i do now to close the previous instance

Gui, Add,  Button , w200 h40 gShowHide , Show/Hide
Gui,show,,testtitle
return

F10::
ShowHide:
toggle:=!toggle
if toggle
Gui, Hide
Else
Gui, show
Return


KillOtherInstances() {	; Kill All other Script.ahk -or- Script.exe instances.               Requires: AhkL, #SingleInstance OFF
   cPID:=DllCall("GetCurrentProcessId"), cName:=A_IsCompiled ? A_ScriptName : ComObjGet("winmgmts:")
   .ExecQuery("Select * from Win32_Process Where " cPID "=ProcessId")._NewEnum()[cPInf] ? cPInf.Name :"" ;AppNane|AutoHotkey.exe
   FOR proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
      IF (proc.Name = cName) && (proc.ProcessID != cPID) && (A_IsCompiled ? True : InStr(proc.CommandLine, A_ScriptFullPath))
         Process, close, % proc.ProcessID
}



GuiClose:
ExitApp


User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Gui restore if its already running and hidden rather than starting a second instance and force closing

22 Mar 2022, 13:09

Code: Select all

Gui, Add, Button, w300, Test
Start:
Gui, Show,, Test
Return

F10::
If WinExist("Test ahk_class AutoHotkeyGUI ahk_exe AutoHotkey.exe")
 Gui, Hide
Else Gosub, Start
Return
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Gui restore if its already running and hidden rather than starting a second instance and force closing

22 Mar 2022, 13:18

mikeyww wrote:
22 Mar 2022, 13:09

Code: Select all

Gui, Add, Button, w300, Test
Start:
Gui, Show,, Test
Return

F10::
If WinExist("Test ahk_class AutoHotkeyGUI ahk_exe AutoHotkey.exe")
 Gui, Hide
Else Gosub, Start
Return
thank you for your reply
how ever , what i am trying to do is .....

There is already an instance of the script is running
and if i by mistake try to relaunch the script again without closing the previous instance, it should not start the new instance, instead it should maximise the hidden window of old instance, and self close the new instance itslef
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Gui restore if its already running and hidden rather than starting a second instance and force closing

22 Mar 2022, 13:28

i tried
method 1
#SingleInstance ignore ; but the problem is the hidden window stay hidden i wont know what is happening without actually checking if the instance is already running or not (mostly hidden to tray)

Method 2
#singleinstance force ; but the problem is it will show message of clould not close previous instance

Method 3
i am at this right now to force close previous instance and start the new one ( But the problem is it is very unnecessary and any unsaved data looses )

method 4
is the one i am looking for ; just the new instance restore gui of old instance and self close itself
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: Gui restore if its already running and hidden rather than starting a second instance and force closing

22 Mar 2022, 13:52

What about:

Code: Select all

#singleinstance, off			; force, ignore, off

if WinExist("activ8")			; if a 2nd instance is launched, activate the existing instance and kill the new one
	{
	WinActivate
	exitapp
	}

gui, add, edit, w300 h300, Hello World
gui, show, ,activ8
return

guiclose:
WinMinimize
return
I use this for a little calculator I have. Works like a charm here. Also prevents closing (and losing edits) by minimizing rather closing.
14.3 & 1.3.7
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Gui restore if its already running and hidden rather than starting a second instance and force closing

23 Mar 2022, 01:42

flyingDman wrote:
22 Mar 2022, 13:52
What about:

Code: Select all

#singleinstance, off			; force, ignore, off

if WinExist("activ8")			; if a 2nd instance is launched, activate the existing instance and kill the new one
	{
	WinActivate
	exitapp
	}

gui, add, edit, w300 h300, Hello World
gui, show, ,activ8
return

guiclose:
WinMinimize
return
I use this for a little calculator I have. Works like a charm here. Also prevents closing (and losing edits) by minimizing rather closing.
its working if its just minimized , but not working if its hidden

Code: Select all

#singleinstance, off			; force, ignore, off


DetectHiddenWindows On
if WinExist("activ8")			; if a 2nd instance is launched, activate the existing instance and kill the new one
	{
	;~ WinRestore, activ8
	WinActivate
	exitapp
	}

gui, add, edit, w300 h300, Hello World
gui, show, ,activ8
return

guiclose:
Gui, Hide
return


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 100 guests