Trying to make advance SingleInstance force Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Trying to make advance SingleInstance force

08 Feb 2018, 09:41

Hi All,

I have a same script in different folders, when i run scripts one by one, they all started running, even though they have same name.

i think it's because of the nature of #SingleInstance force, It cant close the previous script with the same name if it's in the different folder.

I try to do that with the below script i found but it's not working can anyone tell me what i'm doing wrong here.

Code: Select all

#SingleInstance force
#Persistent
SetWorkingDir, %A_ScriptDir%
DetectHiddenWindows, On
SetTitleMatchMode, 2
WinGet, vWinList, List, \Test.ahk ahk_class AutoHotkey
Loop, % vWinList
{
	hWnd := vWinList%A_Index%
	;WinGetTitle, vWinTitle, % "ahk_id " hWnd
	;WinGetClass, vWinClass, % "ahk_id " hWnd
	;WinGet, vPID, PID, % "ahk_id " hWnd
	if !(hWnd = A_ScriptHwnd)
		WinClose, % "ahk_id " hWnd
}
Msgbox, running
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Trying to make advance SingleInstance force

08 Feb 2018, 10:02

If your script has a Gui, you can try this PostMessage, 0x111, 65307,,, NewAutoHotkeyScript.ahk.
I've tested with two scripts and when you look a bit how to match the filenames, it should solve your issue.
Einfach nur ein toller Typ. :mrgreen:
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Trying to make advance SingleInstance force

08 Feb 2018, 10:09

Sorry Divanebaba :( My scripts dose not have GUI they runs in the background. thank you so much for your support.
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Trying to make advance SingleInstance force

08 Feb 2018, 11:35

I tried the link you gave Not able to make sense :roll:
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Trying to make advance SingleInstance force

08 Feb 2018, 15:48

Hi.

There is an nice example at this site. Look Example #5 at the bottom of the site.
Or look here for a bit modified version.

Code: Select all

; Beispiel #5: Ermittelt eine Liste von allen laufenden Prozessen via COM.
Gui, +Caption +Resize MinSize550x150
Gui, Add, ListView, x2 y0 w400 h500 vLV, Prozessname|Befehlszeile
for Prozess in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
     LV_Add("", Prozess.Name, Prozess.CommandLine)	
Gui, Show,, Prozessliste
return

GuiSize:
GuiControl, move, LV, % " w" A_GuiWidth - 4 " h" A_GuiHeight - 16
return
As you can see, in second column there are the paths and filenames listet.
I tried to split the loop to get access to second column, but I failed.
Should be possible to identify a thread with same name with this example.
How to close, I don't know, because I get stucked by the for-loop.

But I think this could be a possible first step.
Maybe the professionals here have not so many problems like I and can show you better and faster way.
Einfach nur ein toller Typ. :mrgreen:
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trying to make advance SingleInstance force

08 Feb 2018, 15:50

If I'm understanding your situation correctly, it's the same that I had and which I addressed with this little function:

Code: Select all

; Check if a script of the same name is already running (from a different location) and, if so, close the older process
CheckProcess()
{
  PID := DllCall("GetCurrentProcessId")
  Process, Exist, %A_ScriptName%
  If (ErrorLevel != PID)
    Process, Close, %ErrorLevel%
}
Put that anywhere and then add CheckProcess() near the beginning (top) of your script.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Trying to make advance SingleInstance force

09 Feb 2018, 05:30

Yatendra3192 wrote:I try to do that with the below script i found but it's not working
Did you name the script Test.ahk? It works for me.
divanebaba wrote:If your script has a Gui, you can try this
Yatendra3192 wrote:Sorry Divanebaba :( My scripts dose not have GUI they runs in the background.
That script does not require a GUI. It posts a message to the script's main window, which every script has.
Yatendra3192
Posts: 89
Joined: 10 Dec 2017, 06:57

Re: Trying to make advance SingleInstance force

09 Feb 2018, 13:55

lexikos wrote:
Yatendra3192 wrote:I try to do that with the below script i found but it's not working
Did you name the script Test.ahk? It works for me.
divanebaba wrote:If your script has a Gui, you can try this
Yatendra3192 wrote:Sorry Divanebaba :( My scripts dose not have GUI they runs in the background.
That script does not require a GUI. It posts a message to the script's main window, which every script has.
find the problem why my script not working

if script name in uppercase (Test.ahk) and under the script it's mentioned in lowercase (test.ahk) it will not work properly.

I don't know why but after fixing this it's working fine.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, Google [Bot], jaka1, mikeyww and 312 guests