Creates or opens a job object
#include <WinAPIProc.au3>
_WinAPI_CreateJobObject ( [$sName = '' [, $tSecurity = 0]] )
$sName | [optional] The name of the job. Name comparison is case-sensitive. If this parameter is '', the job is created without a name. |
$tSecurity | [optional] $tagSECURITY_ATTRIBUTES structure that specifies the security descriptor for the job object and determines whether child processes can inherit the returned handle. If this parameter is 0 (Default), the job object gets a default security descriptor and the handle cannot be inherited. |
Success: | [optional] Handle to the job object. If the object existed before the function call, the function returns a handle to the existing job object. |
Failure: | 0, call _WinAPI_GetLastError() to get extended error information. |
When a job is created, its accounting information is initialized to zero, all limits are inactive, and there are no associated processes. To assign a process to a job object, use the _WinAPI_AssignProcessToJobObject() function.
To set limits for a job, use the _WinAPI_SetInformationJobObject() function.
To query accounting information, use the _WinAPI_QueryInformationJobObject() function.
All processes associated with a job must run in the same session. A job is associated with the session of the first process to be assigned to the job.
To close a job object handle, use the _WinAPI_CloseHandle() function. The job is destroyed when its last handle has been closed and all associated processes have exited.
_WinAPI_AssignProcessToJobObject, _WinAPI_CloseHandle, _WinAPI_QueryInformationJobObject, _WinAPI_SetInformationJobObject
Search CreateJobObject in MSDN Library.
#include <WinAPIConv.au3>
#include <WinAPIHObj.au3>
#include <WinAPIProc.au3>
Local Const $sTemp = @TempDir & '\Test.au3'
; Create temporary .au3 file
Local $hFile = FileOpen($sTemp, 2)
For $i = 1 To 3
FileWriteLine($hFile, 'Run(@SystemDir & "\notepad.exe")' & @CRLF & 'Sleep(100)')
Next
FileClose($hFile)
; Run 3 times the "notepad.exe" and wait until you have closed all 3 processes
_RunWaitEx(@AutoItExe & ' /AutoIt3ExecuteScript "' & $sTemp & '"')
; Delete temporary .au3 file
FileDelete($sTemp)
Func _RunWaitEx($sCmd)
; Original idea by amel27
Local $tProcess = DllStructCreate($tagPROCESS_INFORMATION)
Local $tStartup = DllStructCreate($tagSTARTUPINFO)
Local $tInfo = DllStructCreate($tagJOBOBJECT_BASIC_ACCOUNTING_INFORMATION)
Local $hJob = _WinAPI_CreateJobObject()
If Not $hJob Then Return SetError(1, 0, 0)
DllStructSetData($tStartup, 'Size', DllStructGetSize($tStartup))
If Not _WinAPI_CreateProcess('', $sCmd, 0, 0, 0, BitOR($CREATE_BREAKAWAY_FROM_JOB, $CREATE_SUSPENDED), 0, 0, $tStartup, $tProcess) Then
Return SetError(1, _WinAPI_CloseHandle($hJob), 0)
EndIf
Local $hProcess = DllStructGetData($tProcess, 'hProcess')
Local $hThread = DllStructGetData($tProcess, 'hThread')
_WinAPI_AssignProcessToJobObject($hJob, $hProcess)
_WinAPI_ResumeThread($hThread)
_WinAPI_CloseHandle($hThread)
Do
If Not _WinAPI_QueryInformationJobObject($hJob, 1, $tInfo) Then
ExitLoop
EndIf
Sleep(100)
Until Not DllStructGetData($tInfo, 'ActiveProcesses')
_WinAPI_CloseHandle($hProcess)
_WinAPI_CloseHandle($hJob)
Return 1
EndFunc ;==>_RunWaitEx
Func _WinAPI_ResumeThread($hThread)
Local $aRet = DllCall('kernel32.dll', 'dword', 'ResumeThread', 'ptr', $hThread)
If @error Or (_WinAPI_DWordToInt($aRet[0]) = -1) Then Return SetError(1, 0, -1)
Return $aRet[0]
EndFunc ;==>_WinAPI_ResumeThread