Tech Tip: Code Sample: Finding processes matching a search term
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac & Win
Published On: December 6, 2010
The following method can be used to return information about all running processes matching a specific search term:
// Description: // get information about processes matching a search term // Parameters: // $1 = text; find this value(optional: show all if omitted) // $2 = pointer->array string(31); process name (optional) // $3 = pointer->array integer; process number (optional) // $4 = pointer->array longint; ticks taken by process (optional) // Return Value: // $0 = longint; number of matching processes // C_LONGINT($0) // number of running processes matching term C_LONGINT($vlParams;$vlNbTasks;$vlActualCount) $vlParams:=Count parameters $vlNbTasks:=Count tasks //total tasks $vlActualCount:=0 // num matching If($vlParams>0) C_TEXT($1;$vsFind) $vsFind:=$1 // find this If($vlParams>1) C_POINTER($2) ARRAY STRING(31;$2->;$vlNbTasks) End if If($vlParams>2) C_POINTER($3) ARRAY INTEGER($3->;$vlNbTasks) End if If($vlParams>3) C_POINTER($4) ARRAY LONGINT($4->;$vlNbTasks) End if C_LONGINT($vlProcess) For($vlProcess;1;$vlNbTasks) C_LONGINT($vlState;$vlTicks) C_TEXT($vsName) PROCESS PROPERTIES($vlProcess;$vsName;$vlState;$vlTicks) If(Position($vsFind;$vsName)#0) // found search string in running process $vlActualCount:=$vlActualCount+1 If($vlParams>1) $2->{$vlActualCount}:=$vsName End if If($vlParams>2) $3->{$vlActualCount}:=$vlProcess End if If($vlParams>3) $4->{$vlActualCount}:=$vlTicks End if End if End for // Eliminate unused extra elements If($vlParams>1) ARRAY STRING(31;$2->;$vlActualCount) End if If($vlParams>2) ARRAY INTEGER($3->;$vlActualCount) End if If($vlParams>3) ARRAY LONGINT($4->;$vlActualCount) End if // return number of matching processes $0:=$vlActualCount Else // no params - return total number of tasks $0:=$vlNbTasks End if |
When the above method is saved as UTIL_processCountMatching it can be used like this:
C_LONGINT($num) ARRAY STRING(31;$procNames;0) ARRAY INTEGER($procNums;0) ARRAY LONGINT($procTicks;0) $num:=UTIL_processCountMatching("_loop_test";->$procNames;->$procNums;->$procTicks) |