Process¶
Inherited: Object
Description¶
This class provides a cross-platform interface for launching processes, reading standard output and error, waiting for start and finish, and managing the child process environment and working directory.
Example:
Process p;
p.start("myapp", {"--option", "value"});
if (p.waitForStarted(5000)) {
// ...
}
Public Methods¶
Process () |
|
~Process () |
|
void |
errorOccurred (int error) |
int |
exitCode () const |
void |
finished (int exitCode) |
bool |
isRunning () const |
void |
kill () |
void |
|
void |
|
void |
setProcessEnvironment (const ProcessEnvironment & environment) |
void |
setWorkingDirectory (const TString & directory) |
bool |
start (const TString & program, const StringList & arguments) |
state () const |
|
void |
terminate () |
bool |
waitForFinished (int timeoutMs = -1) |
bool |
waitForStarted (int timeoutMs = -1) |
Public Enums¶
enum Process::Error
Error codes returned or emitted by the Process.
Constant |
Value |
Description |
Process::FailedToStart |
0 |
Failed to launch the child process. |
Process::Crashed |
1 |
The child process crashed or was terminated by a signal. |
Process::Timedout |
2 |
An operation timed out. |
Process::ReadError |
3 |
Error occurred while reading from process pipes. |
Process::WriteError |
4 |
Error occurred while writing to process stdin. |
Process::UnknownError |
5 |
An unspecified error occurred. |
enum Process::State
Process lifecycle state. Describes the current lifecycle stage of the managed child process.
Constant |
Value |
Description |
Process::NotRunning |
0 |
no process is running. |
Process::Starting |
1 |
process start has been initiated. |
Process::Running |
2 |
process is currently running. |
Process::Finished |
3 |
process has terminated. |
Static Methods¶
bool |
openUrl (const TString & url) |
bool |
startDetached (const TString & program, const StringList & arguments, const TString & workingDirectory, const ProcessEnvironment & environment) |
Methods Description¶
Process::Process ()
Construct a new Process object.
Process::~Process ()
Destructor; terminates running process and frees resources.
void Process::errorOccurred (int error)
Emit an signal with the given error code.
The error parameter is one of the Process::Error enumeration values.
int Process::exitCode () const
Retrieve the exit code of the finished process.
If the process has finished, returns its exit code; otherwise returns the last known exit code value (or -1 if unknown).
void Process::finished (int exitCode)
Emit signal when the process exits.
The exitCode parameter contains the child’s exit status.
bool Process::isRunning () const
Check whether the process is currently running.
Convenience helper returning true when the internal state is Process::State::Running.
void Process::kill ()
This unconditionally terminates the process. Use only when terminate() did not cause the process to exit in a timely manner.
bool Process::openUrl (TString & url)
Open the given url using the system default handler; returns true if succeed.
TString Process::readAllStandardError ()
Return and clear the accumulated standard error buffer.
Returns all data read from the child process’s stderr since the last call and clears the internal buffer.
TString Process::readAllStandardOutput ()
Return and clear the accumulated standard output buffer.
Returns all data read from the child process’s stdout since the last call and clears the internal buffer.
void Process::readyReadStandardError ()
Emit when stderr data is available.
void Process::readyReadStandardOutput ()
Emit when stdout data is available.
void Process::setProcessEnvironment (ProcessEnvironment & environment)
Set environment variables for the child process.
void Process::setWorkingDirectory (TString & directory)
Set the working directory for the process to be started.
bool Process::start (TString & program, StringList & arguments)
Start a process and begin monitoring it.
Attempts to start the specified program with arguments. On success the process state becomes Running and a background monitor thread is spawned to read output and detect process termination.
Returns true on success, false otherwise.
bool Process::startDetached (TString & program, StringList & arguments, TString & workingDirectory, ProcessEnvironment & environment)
Start a detached process (no parent-child ties to this process).
Starts program with arguments in a separate process group/session and returns immediately. Optionally sets the workingDirectory and the provided environment variables for the new process.
Returns true if the detached process was launched successfully.
Process::State Process::state () const
Get the current process state.
Returns one of the Process::State enumeration values describing the lifecycle state of the child process.
void Process::terminate ()
Sends a termination request to the process. This is a graceful request; it does not force immediate termination.
bool Process::waitForFinished (int timeoutMs = -1)
Wait for the process to finish.
Blocks until the monitored process has finished or until timeoutMs milliseconds have passed. A negative timeoutMs will wait without polling and join the monitor thread.
Returns true if the process has finished.
bool Process::waitForStarted (int timeoutMs = -1)
Wait for the process to start.
Blocks until the process has started or the optional timeoutMs milliseconds have elapsed. A negative timeoutMs means wait forever. Returns true if the process started successfully.