ProcessGetState Function

Action

Returns the state of the specified process.

Important: This function must be called after the ProcessInitialize function.
Note: This function can be called more than once (for every environment variable).

Include file

Kernel.bdh

Syntax

ProcessGetState( in hProc : number ): number;

Return value

If the function is successful, the status of the process is returned, which can be one of the following:

  • PROCESS_STATE_INITIALIZED : The process is initialized, but not started yet.

  • PROCESS_STATE_EXECUTING : The process was started and is now running.

  • PROCESS_STATE_FINISHED : The process has finished successfully.

  • PROCESS_STATE_TIMEDOUT : The process has timed out, which means that it was started attached (or piped) and did not respond within a finite time.

In case of an error 0 is returned.

Parameter Description
hProc The handle that identifies the process. It is returned by the ProcessInitialize function.

Example

dcltrans
  transaction TMain
  var
    hFile      : number;
    hProcessId : number;
    nStatus    : number;
    nRead      : number;
    sData      : string;
  begin
    // Create process and start it up...
    hProcessId := ProcessInitialize ("application1.exe", PROCESS_DETACHED,
                        "", "c:\\temp\\", "c:\\temp\\out.txt");
    ProcessSetEnv(hProcessId, "temp", "c:\\temp\\");
    ProcessStart(hProcessId);

    // Wait for process to finish
    Wait(25.0);

    // Kill the process if it hasn't finished yet
    nStatus := ProcessGetState(hProcessId);
    if (nStatus <> PROCESS_STATE_FINISHED) then
      ProcessKill(hProcessId,PROCESS_KILLSINGLE);
    end;

    // Read the output of the process, written to a file
    FOpen(hFile, "c:\\temp\\out.txt", OPT_FILE_ACCESS_READ, OPT_FILE_OPEN);
    while FRead(hFile, sData, 1, nRead) do
      Write(sData);
    end;

    FClose(hFile);
    ProcessFree(hProcessID);
  end TMain;