将AutoIt v3脚本编译为EXE可执行文件后,我想确保一次只能运行一个实例。实现此目标的最佳方法是什么?
答案 0 :(得分:1)
更新:我认为公认的答案中的单例函数是一种更可靠的方法。但是,如果有较旧版本的人可能无法使用它,或者可能希望使用其他方法,那么我将在此处保留此答案。
这些代码行将允许您退出已运行的脚本。当脚本被编译成EXE时,效果最佳。请注意,如果它作为未编译的AU3脚本运行,并且 any AutoIt脚本已经在运行,它将退出。
希望这对某人有帮助。
#include <Process.au3>
; Quit with message box if app is already running
$numberOfAppInstances = ProcessList(_ProcessGetName(@AutoItPID))
$numberOfAppInstances = $numberOfAppInstances[0][0]
If $numberOfAppInstances > 1 Then
MsgBox(0, "Exiting", "Application already running!")
Exit
EndIf
答案 1 :(得分:1)
您需要的是_Singleton
#include <Misc.au3>
#include <MsgBoxConstants.au3>
If _Singleton("test", 1) = 0 Then
MsgBox($MB_SYSTEMMODAL, "Warning", "An occurrence of test is already running")
Exit
EndIf
MsgBox($MB_SYSTEMMODAL, "OK", "the first occurrence of test is running")