Jump to content

Trap second instance of running script


Recommended Posts

In my thread entitled Newbie template for assigning script to hotkey, I was rightly chastised for failing to include error-checking that would trap an attempt to launch a second instance of the script, which then would not be responsive to the hotkey for terminating the script. Forum member Exit set me straight with his _Hotkey function, which does just that (and which I've incorporated into my "template"):

; Code by forum member Exit
_HotKey("{ESC}")
_HotKey("^b")
Func _HotKey($hotkey = "")
;       ! ALT  + SHIFT  ^ CONTROL  # WinKey

    Switch @HotKeyPressed

        Case "{ESC}"
            Exit 0*MsgBox(64 + 262144, Default, "Exit", 1)
        Case "^b"
            Beep()
            ; delete ";" to temporarely disable hotkey

            ; HotKeySet(@HotKeyPressed)
            ; send("^b")
            ; HotKeySet(@HotKeyPressed,"_Hotkey")
        Case Else
            If Not IsDeclared("hotkey") Then Return MsgBox(16 + 262144, Default, "No CASE statement defined for hotkey " & @HotKeyPressed)
            If HotKeySet($hotkey, "_Hotkey") = 0 Then Return MsgBox(16 + 262144, Default, "Hotkey " & $hotkey & " invalid or set by another application.")
    EndSwitch

EndFunc   ;==>_HotKey



While Sleep(100)     ; here should be your application.
WEnd                 ; meanwhile, here is a dummy loop.

It occurred to me that an alternate way of doing this, involving less code, might be:

#include <Misc.au3>

If _Singleton(@ScriptName, 1) = 0 Then
    MsgBox(64, @ScriptName, @ScriptName & " is already running!", 2)
    Exit
EndIf

Are there advantages/disadvantages to this alternate method?

Thanks in advance for your help.

PS: Private message sent to Exit alerting him to this thread.

Edited by CarlD
Link to comment
Share on other sites

It occurred to me that an alternate way of doing this, involving less code, might be:

#include <Misc.au3>
If _Singleton(@ScriptName, 1) = 0 Then MsgBox(64, @ScriptName, @ScriptName & " is already running!", 2)
     Exit
EndIf

Are there advantages/disadvantages to this alternate method?

This way does not help​ when the hotkey is set by another application or by system.
Just try to set F12. It is reserved to system. See also help file.

_HotKey("{ESC}")
_HotKey("{F12}")
Func _HotKey($hotkey = "")
    Switch @HotKeyPressed

        Case "{ESC}"
            Exit 0 * MsgBox(64 + 262144, Default, "Exit", 1)
        Case "{F12}"
            Beep()
        Case Else
            If Not IsDeclared("hotkey") Then Return MsgBox(16 + 262144, Default, "No CASE statement defined for hotkey " & @HotKeyPressed)
            If HotKeySet($hotkey, "_Hotkey") = 0 Then Return MsgBox(16 + 262144, Default, "Hotkey " & $hotkey & " invalid or set by another application.")
    EndSwitch

EndFunc   ;==>_HotKey



While Sleep(100) ; here should be your application.
WEnd ; meanwhile, here is a dummy loop.

 

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Exit, thanks. I also noticed that your function actually prevents a second instance of the script from opening, whereas the _Singleton statement takes effect in the second instance (but quickly shuts it down). Your _Hotkey function is very instructive -- thanks again.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...