Comment using .NET to test if a process exists.

Cecil

19+ years progress programming and still learning.
This is a simple test using .NET to see if a process is already running. Before, I was using tasklist command and then parsing the output. This code could be adapted to include more information. However, I have not figured out how to cast a .NET array to an ABL extent yet.

Code:
USING System.Diagnostics.*.

FUNCTION isProcessExist RETURNS LOGICAL (INPUT processName AS CHARACTER):

    DEFINE VARIABLE strArr AS CLASS System.Collections.IList NO-UNDO.
    DEFINE VARIABLE sysProcesses AS CLASS "System.Diagnostics.PROCESS[]".
  
    sysProcesses = System.Diagnostics.Process:GetProcessesByName(INPUT processName).

    RETURN LOGICAL( sysProcesses:LENGTH ).

END FUNCTION.

MESSAGE isProcessExist("notepad").
 

Cecil

19+ years progress programming and still learning.
the function also displays information about the running process.
REF: Process.Responding Property (System.Diagnostics)

Code:
USING System.Diagnostics.*.

FUNCTION isProcessExist RETURNS LOGICAL (INPUT processName AS CHARACTER):

       DEFINE VARIABLE sysProcesses AS CLASS "System.Diagnostics.Process[]".
       DEFINE VARIABLE sysProcess   AS CLASS System.Diagnostics.Process.
      
       DEFINE VARIABLE i AS INTEGER     NO-UNDO.
    
    sysProcesses = System.Diagnostics.Process:GetProcessesByName(INPUT processName).
    
    DO i = 0 TO sysProcesses:LENGTH - 1:
        sysProcess = CAST(sysProcesses:GetValue(i), System.Diagnostics.Process).
    
        MESSAGE sysProcess:ProcessName sysProcess:Id sysProcess:TotalProcessorTime sysProcess:Responding .
    
    END.

    RETURN LOGICAL( sysProcesses:LENGTH ).

END FUNCTION.

MESSAGE isProcessExist("notepad").
 

RealHeavyDude

Well-Known Member
As far as I know you can't directly cast a .NET array to ABL extents. You need to capture the output as .NET System.Array, iterate over the array and cast each individual member to what you expect.

Code:
define variable currentDomain  as System.AppDomain            no-undo.
define variable assemblies     as System.Array                no-undo.
define variable assembly       as System.Reflection.Assembly  no-undo.
define variable assemblyNo     as integer     no-undo.
/* Grab the current domain */
assign currentDomain = System.AppDomain:CurrentDomain.
/* Get an array of currently loaded assemblies */
assign assemblies = currentDomain:GetAssemblies ( ).
/* Loop through the array of assemblies */
do assemblyNo = 1 to assemblies:Length:
    assign assembly = cast ( assemblies:GetValue ( assemblyNo - 1 ), System.Reflection.Assembly ).
    infoMessage ( substitute ( 'ASSEMBLY: &1.', assembly:FullName ) ).
end.

Something like the above for example.
 
Top