Code: Select all
; 示例 - 允许 5 个记事本同时运行
Loop, 10 {
mProcess.wait(5) ; 等待进程小于 5 个
Run, notepad,,, pid ; 运行 notepad.exe,获取 PID
mProcess.addPID(pid) ; 记录这个 PID
}
mProcess.waitFinish() ; 等待所有进程结束,也可以用 mProcess.wait(1)
MsgBox, finish!
Return
class mProcess
{
static pidList := []
; 等待进程小于指定数量
wait(MaxRun = 1) {
Loop {
count := 0
newList := []
For i, pid in this.pidList
{
Process, Exist, %pid%
If ErrorLevel
count ++, newList.Insert(pid)
}
If (!count || count < MaxRun)
Break
Else
Sleep, 1000
}
this.pidList := newList
}
addPID(pid) {
this.pidList.Insert(pid)
}
waitFinish() {
this.wait(1)
}
}