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:

Instance()

This call will direct the script into the Label: subroutine when a command line parameter matches your Label, and override the rest of the autoexec section

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 (Default "-")

Related

#SingleInstance, Thread(), Exit, ExitApp

Example

; this example will use the SoundBeep command normally or with Instance()
#NoEnv
Instance()  ; checks for parameters/label
; and redirects auto-execute section to the %1% label
 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