Page 1 of 1

Trying to make advance SingleInstance force

Posted: 08 Feb 2018, 09:41
by Yatendra3192
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

Re: Trying to make advance SingleInstance force

Posted: 08 Feb 2018, 10:02
by divanebaba
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.

Re: Trying to make advance SingleInstance force

Posted: 08 Feb 2018, 10:09
by Yatendra3192
Sorry Divanebaba :( My scripts dose not have GUI they runs in the background. thank you so much for your support.

Re: Trying to make advance SingleInstance force

Posted: 08 Feb 2018, 11:35
by Yatendra3192
I tried the link you gave Not able to make sense :roll:

Re: Trying to make advance SingleInstance force

Posted: 08 Feb 2018, 15:48
by divanebaba
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.

Re: Trying to make advance SingleInstance force

Posted: 08 Feb 2018, 15:50
by Osprey
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.

Re: Trying to make advance SingleInstance force

Posted: 09 Feb 2018, 05:30
by lexikos
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.

Re: Trying to make advance SingleInstance force

Posted: 09 Feb 2018, 13:55
by Yatendra3192
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.

Re: Trying to make advance SingleInstance force  Topic is solved

Posted: 10 Feb 2018, 16:53
by lexikos
Window titles are case-sensitive.