toggle SingleInstance Mode

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

toggle SingleInstance Mode

24 Jun 2015, 10:06

For my script, I want to toggle single-instance mode.

So I came up with this, but something is wrong:
1. If I don't wrap the if statement in brackets, AutoHotkey says I have an "else" without an "if" statement.
2. I think that Autohotkey is reading both declarations.

So I'm thinking this doesn't work.

Code: Select all

instancemode := true
gui, add,text,,instancemode = %instancemode%
gui, show
SetInstanceMode()	
return

 
SetInstanceMode(){
	global
	if(instancemode){
		#SingleInstance, force
	}else{
		#SingleInstance, off
	}
}
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: toggle SingleInstance Mode

24 Jun 2015, 11:28

Directives are processed when the script loads, they can't be conditional.
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: toggle SingleInstance Mode

24 Jun 2015, 12:03

I tried to think of a workaround for this. My idea involved reading and writing an external file when opening and exiting the script. If script was running, value in external file would be 1, and then 0 when you exit the script, etc. When you run the script it would first read this value, and would exit itself if already running, etc.

Process, Exist could possibly be used depending on the script, maybe?
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: toggle SingleInstance Mode

24 Jun 2015, 14:42

I've needed this functionality and implemented it script-side in Instance() and in EitherMouse with windows messages...

Code: Select all

Instance() ;near top of auto-execute section

Code: Select all

; Instance() - 0.43 - gwarble
;  creates another instance to perform a task
;    help: http://www.autohotkey.com/forum/viewtopic.php?p=310694
;
; Instance(Label,Params,AltWM)
;
;  Label   "" to initialize, should be in autoexecute section as: [ If Instance("","-") ]
;          "Label" to start a new instance, whose execution       [   Return            ]
;            will be redirected to the "Label" subroutine
;
;  Params  parameters to pass to new instance (retrieved normally, ie: %2%)
;          when initializing (ie: label="") Params is a label prefix for all calls
;          last param received by script is calling-instance's ProcessID
;
;  AltWM   Alternate WindowsMessage number if 0x1357 (arbitrary anyway) will conflict
;
;  Return  ProcessID of new instance
;
Instance(Label="", Params="", WM="0x1357") {
 global 1		; uses first command line parameter to redirect auto-execute section
 If Label =             ; called from autoexec section with Label="" to redirect new instances
 {
  Label = %1%
  If InStr(Label, Params)
  {
   If IsLabel(Label)
   {
    GoSub, %Label%
    Exit                ; don't run the rest of autoexecute section
   }
  }
;  Else                  ; otherwise this will make it act as [ SingleInstance, force ]
;  {                     ; unless second param is "Single"
   pDHW := A_DetectHiddenWindows
   DetectHiddenWindows, On
   WinGet, Instance_ID, List, %A_ScriptFullPath%
   If (Params <> "Single")
    Loop %Instance_ID%
     SendMessage, WM, WM, 0, , % "ahk_id " Instance_ID%A_Index%
   DetectHiddenWindows, %pDHW%
   OnMessage(WM, "Instance_")
;  }
  Return
 }
 Else
 {
  If IsLabel(Label)
  {
   ProcessID := DllCall("GetCurrentProcessId")
   If A_IsCompiled
    Run, "%A_ScriptFullPath%" /f "%Label%" %Params% %ProcessID%,,,Instance_PID
   Else
    Run, "%A_AhkPath%" /f "%A_ScriptFullPath%" "%Label%" %Params% %ProcessID%,,,Instance_PID
   Return %Instance_PID%
  }
  Return
 }
 #SingleInstance, Off 	; your script needs this anyway for Instance() to be useful, so its here
}

Instance_(wParam, lParam) {	; OnMessage Handler for singleInstance behavior or to
 Critical			; send messages back to calling instance via subroutine to run
 If lParam = 0			; (ie status updates, "i'm finished", etc)
  ExitApp			; if message lparam sent back besides 0, calling
 Else If IsLabel(Label := "Instance_" lParam) ; ; instance looks for and runs label Instance_%lParam%
  GoSub, %Label%		; so the script would have a label like:
 Return				; Instance_1: and Instance_2: or Instance_%Integer%:
}				; and the new instance can SendMsg,WM,WM,Integer to the calling instance

- gwarble
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Rohwedder and 241 guests