mProcess - 多进程运行

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

mProcess - 多进程运行

15 Nov 2014, 03:24

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)
	}
}

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 2 guests