Instance()

gwarble's Tools
Download

Create another instance/process of the running script, to perform an asynchronous task without interrupting the main script.

Instance(["Label","Params"])

This function should be called once from the auto-execute section without any parameters:

If Instance()
Return   ; if a new instance request is found, stop normal auto-execution

This call will direct the script into the Label: subroutine when a command line parameter matches your Label.

Elsewhere in your script, a new instance can be created to perform some task, by calling Instance("Label"). The Label subroutine will be run in the new instance.

Parameters

Label

[None] - checks first command-line parameter and starts new instance if label found

Label - Label name/subroutine ran when called (in second instance)

Params

[None] - any extra params to be sent to Label in new instance.
These can be accessed in Label: as normal, %2%...

Prefix - when initializing (ie: Label=""), a prefix can be used to isolate label names and stop conflicts

Related

#SingleInstance, Thread(), Exit, ExitApp

Example

; this example will use the SoundBeep command normally or with Instance()
#SingleInstance, Off   ; recommended, use OnMessage to act like Force
 If Instance("","-")  ; checks for parameters/label
  Return
 Gui, Add, Button, gSynchronous,  Instance("-Beep")
 Gui, Add, Button, gBeep, SoundBeep
 Gui, Show
Return

Synchronous:
   Instance("-Beep") ; starts another instance and runs the beep label
Return

-Beep:  ; this will be run by the second instance, synchronously
   TrayTip, Beep..., Secondary Instance ; without pausing your script
   SoundBeep, 500, 1000
ExitApp

Beep:   ; normal asnchronous behavior of SoundBeep
   TrayTip, Beep..., Main Instance
   SoundBeep, 500, 1000
   TrayTip
Return

AutoHotkey Homepage  |  gwarble's Tools  |  Download Instance.ahk