Performs one of the following operations on a process: checks if it exists; changes its priority; closes it; waits for it to close.
Process, Cmd, PID-or-Name [, Param3] |
Parameters
Cmd | One of the following words: Exist: Sets ErrorLevel to the Process ID (PID) if a matching process exists, or 0 otherwise. In v1.0.36+, if the PID-or-Name parameter is blank, the script's own PID is retrieved. Close: If a matching process is successfully terminated, ErrorLevel is set to its former Process ID (PID). Otherwise (there was no matching process or there was a problem terminating it), it is set to 0. Since the process will be abruptly terminated -- possibly interrupting its work at a critical point or resulting in the loss of unsaved data in its windows (if it has any) -- this method should be used only if a process cannot be closed by using WinClose on one of its windows. Priority: Changes the priority (as seen in Windows Task Manager) of the first matching process to Param3 and sets ErrorLevel to its Process ID (PID). If the PID-or-Name parameter is blank, the script's own priority will be changed. If there is no matching process or there was a problem changing its priority, ErrorLevel is set to 0. Param3 should be one of the following letters or words: L (or Low), B (or BelowNormal), N (or Normal), A (or AboveNormal), H (or High), R (or Realtime). Since BelowNormal and AboveNormal are not supported on Windows 95/98/Me/NT4, normal will be automatically substituted for them on those operating systems. Note: Any process not designed to run at Realtime priority might reduce system stability if set to that level. Wait: Waits up to Param3 seconds (can contain a decimal point) for a matching process to exist. If Param3 is omitted, the command will wait indefinitely. If a matching process is discovered, ErrorLevel is set to its Process ID (PID). If the command times out, ErrorLevel is set to 0. WaitClose: Waits up to Param3 seconds (can contain a decimal point) for ALL matching processes to close. If Param3 is omitted, the command will wait indefinitely. If all matching processes are closed, ErrorLevel is set to 0. If the command times out, ErrorLevel is set to the Process ID (PID) of the first matching process that still exists. |
PID-or-Name | This parameter can be either a number (the PID) or a process name as described below. It can also be left blank to change the priority of the script itself. PID: The Process ID, which is a number that uniquely identifies one specific process (this number is valid only during the lifetime of that process). The PID of a newly launched process can be determined via the Run command. Similarly, the PID of a window can be determined with WinGet. The Process command itself can also be used to discover a PID. Name: The name of a process is usually the same as its executable (without path), e.g. notepad.exe or winword.exe. Since a name might match multiple running processes, only the first process will be operated upon. The name is not case sensitive. |
Param3 | See Cmd above for details. |
ErrorLevel
See Cmd above for details.
Remarks
For Wait and WaitClose: Processes are checked every 100 milliseconds; the moment the condition is satisfied, the command will cease waiting. In other words, rather than waiting for the timeout to expire, it will immediately set ErrorLevel as described above and then continue execution of the script. Also, while the command is in a waiting state, new threads can be launched via hotkey, custom menu item, or timer.
To work under Windows NT4, this command requires the file PSAPI.DLL, which is normally already present in the AutoHotkey installation directory (i.e. no extra installation steps should be needed even for NT). However, to use the Process command from within a compiled script on Windows NT4, include a copy of PSAPI.DLL in the same folder as the script or in one of the directories in the system's PATH.
Related
Run, WinGet, WinClose, WinKill, WinWait, WinWaitClose, IfWinExist
Examples
; Example #1: Run Notepad.exe, , , NewPID Process, priority, %NewPID%, High MsgBox The newly launched notepad's PID is %NewPID%.
; Example #2: Process, wait, Notepad.exe, 5.5 NewPID = %ErrorLevel% ; Save the value immediately since ErrorLevel is often changed. if NewPID = 0 { MsgBox The specified process did not appear within 5.5 seconds. return } ; Otherwise: MsgBox A matching process has appeared (Process ID is %NewPID%). Process, priority, %NewPID%, Low Process, priority, , High ; Have the script set itself to high priority. WinClose Untitled - Notepad Process, WaitClose, %NewPID%, 5 if ErrorLevel <> 0 ; The PID still exists. MsgBox The process did not close within 5 seconds.
; Example #3: A hotkey to change the priority of the active window's process: #z:: ; Win+Z hotkey WinGet, active_pid, PID, A WinGetTitle, active_title, A Gui, 5:Add, Text,, Press ESCAPE to cancel, or double-click a new`npriority level for the following window:`n%active_title% Gui, 5:Add, ListBox, vMyListBox gMyListBox r5, Normal|High|Low|BelowNormal|AboveNormal Gui, 5:Add, Button, default, OK Gui, 5:Show,, Set Priority return 5GuiEscape: 5GuiClose: Gui, Destroy return MyListBox: if A_GuiControlEvent <> DoubleClick return ; else fall through to the next label: 5ButtonOK: GuiControlGet, MyListBox Gui, Destroy Process, Priority, %active_pid%, %MyListBox% if ErrorLevel = 0 MsgBox Error: Its priority could not be changed to "%MyListBox%". else MsgBox Success: Its priority was changed to "%MyListBox%". return