# Process Control
[Functions and Macros by Category]

## Detailed Description

The functions in this section are used to launch or terminate the other processes.

## Classes

struct | wxExecuteEnv
| This structure can optionally be passed to wxExecute() to specify additional options to use for the child process. More...

## Functions

void | wxExit ()
| Exits application after calling wxApp::OnExit.
long | wxExecute (const wxString &command, int flags=wxEXEC_ASYNC, wxProcess *callback=NULL, const wxExecuteEnv *env=NULL)
| Executes another program in Unix or Windows.
long | wxExecute (char **argv, int flags=wxEXEC_ASYNC, wxProcess *callback=NULL, const wxExecuteEnv *env=NULL)
| This is an overloaded version of wxExecute(const wxString&,int,wxProcess*), please see its documentation for general information.
long | wxExecute (wchar_t **argv, int flags=wxEXEC_ASYNC, wxProcess *callback=NULL, const wxExecuteEnv *env=NULL)
long | wxExecute (const wxString &command, wxArrayString &output, int flags=0, const wxExecuteEnv *env=NULL)
| This is an overloaded version of wxExecute(const wxString&,int,wxProcess*), please see its documentation for general information.
long | wxExecute (const wxString &command, wxArrayString &output, wxArrayString &errors, int flags=0, const wxExecuteEnv *env=NULL)
| This is an overloaded version of wxExecute(const wxString&,int,wxProcess*), please see its documentation for general information.
unsigned long | wxGetProcessId ()
| Returns the number uniquely identifying the current process in the system.
int | wxKill (long pid, wxSignal sig=wxSIGTERM, wxKillError *rc=NULL, int flags=wxKILL_NOCHILDREN)
| Equivalent to the Unix kill function: send the given signal sig to the process with PID pid.
bool | wxShell (const wxString &command=wxEmptyString)
| Executes a command in an interactive shell window.
bool | wxShutdown (int flags=wxSHUTDOWN_POWEROFF)
| This function shuts down or reboots the computer depending on the value of the flags.

## Function Documentation

long wxExecute | ( | const wxString & | command,
|  | int | flags = `wxEXEC_ASYNC`,
|  | wxProcess * | callback = `NULL`,
|  | const wxExecuteEnv * | env = `NULL` |
| ) |  |  |

Executes another program in Unix or Windows.

In the overloaded versions of this function, if flags parameter contains `wxEXEC_ASYNC` flag (the default), flow of control immediately returns. If it contains `wxEXEC_SYNC`, the current application waits until the other program has terminated.

In the case of synchronous execution, the return value is the exit code of the process (which terminates by the moment the function returns) and will be -1 if the process couldn't be started and typically 0 if the process terminated successfully. Also, while waiting for the process to terminate, wxExecute() will call wxYield(). Because of this, by default this function disables all application windows to avoid unexpected reentrancies which could result from the users interaction with the program while the child process is running. If you are sure that it is safe to not disable the program windows, you may pass `wxEXEC_NODISABLE` flag to prevent this automatic disabling from happening.

For asynchronous execution, however, the return value is the process id and zero value indicates that the command could not be executed. As an added complication, the return value of -1 in this case indicates that we didn't launch a new process, but connected to the running one (this can only happen when using DDE under Windows for command execution). In particular, in this case only, the calling code will not get the notification about process termination.

If callback isn't NULL and if execution is asynchronous, wxProcess::OnTerminate() will be called when the process finishes. Specifying this parameter also allows you to redirect the standard input and/or output of the process being launched by calling wxProcess::Redirect(). If the child process IO is redirected, under Windows the process window is not shown by default (this avoids having to flush an unnecessary console for the processes which don't create any windows anyhow) but a `wxEXEC_NOHIDE` flag can be used to prevent this from happening, i.e. with this flag the child process window will be shown normally.

Under Unix the flag `wxEXEC_MAKE_GROUP_LEADER` may be used to ensure that the new process is a group leader (this will create a new session if needed). Calling wxKill() passing wxKILL_CHILDREN will kill this process as well as all of its children (except those which have started their own session).

The `wxEXEC_NOEVENTS` flag prevents processing of any events from taking place while the child process is running. It should be only used for very short-lived processes as otherwise the application windows risk becoming unresponsive from the users point of view. As this flag only makes sense with `wxEXEC_SYNC`, `wxEXEC_BLOCK` equal to the sum of both of these flags is provided as a convenience.

Note:
  Currently wxExecute() can only be used from the main thread, calling this function from another thread will result in an assert failure in debug build and won't work.

Parameters:

  | command | The command to execute and any parameters to pass to it as a single string, i.e. "emacs file.txt".
  | flags | Must include either wxEXEC_ASYNC or wxEXEC_SYNC and can also include wxEXEC_NOHIDE, wxEXEC_MAKE_GROUP_LEADER (in either case) or wxEXEC_NODISABLE and wxEXEC_NOEVENTS or wxEXEC_BLOCK, which is equal to their combination, in wxEXEC_SYNC case.
  | callback | An optional pointer to wxProcess.
  | env | An optional pointer to additional parameters for the child process, such as its initial working directory and environment variables. This parameter is available in wxWidgets 2.9.2 and later only.

See also:
  wxShell(), wxProcess, External Program Execution Sample, wxLaunchDefaultApplication(), wxLaunchDefaultBrowser()

Include file:

    #include <wx/utils.h>

wxPerl Note: In wxPerl this function is called `Wx::ExecuteCommand`.

long wxExecute | ( | wchar_t ** | argv,
|  | int | flags = `wxEXEC_ASYNC`,
|  | wxProcess * | callback = `NULL`,
|  | const wxExecuteEnv * | env = `NULL` |
| ) |  |  |

long wxExecute | ( | const wxString & | command,
|  | wxArrayString & | output,
|  | int | flags = `0`,
|  | const wxExecuteEnv * | env = `NULL` |
| ) |  |  |

This is an overloaded version of wxExecute(const wxString&,int,wxProcess*), please see its documentation for general information.

This version can be used to execute a process (always synchronously, the contents of flags is or'd with `wxEXEC_SYNC`) and capture its output in the array output.

Parameters:

  | command | The command to execute and any parameters to pass to it as a single string.
  | output | The string array where the stdout of the executed process is saved.
  | flags | May include wxEXEC_NOHIDE, wxEXEC_MAKE_GROUP_LEADER (in either case) or wxEXEC_NODISABLE and wxEXEC_NOEVENTS or wxEXEC_BLOCK, which is equal to their combination. wxEXEC_SYNC is always implicitly added to the flags.
  | env | An optional pointer to additional parameters for the child process, such as its initial working directory and environment variables. This parameter is available in wxWidgets 2.9.2 and later only.

See also:
  wxShell(), wxProcess, External Program Execution Sample, wxLaunchDefaultApplication(), wxLaunchDefaultBrowser()

Include file:

    #include <wx/utils.h>

wxPerl Note: This function is called `Wx::ExecuteStdout:` it only takes the command argument, and returns a 2-element list (`status`, `output`), where `output` in an array reference.

long wxExecute | ( | char ** | argv,
|  | int | flags = `wxEXEC_ASYNC`,
|  | wxProcess * | callback = `NULL`,
|  | const wxExecuteEnv * | env = `NULL` |
| ) |  |  |

This is an overloaded version of wxExecute(const wxString&,int,wxProcess*), please see its documentation for general information.

This version takes an array of values: a command, any number of arguments, terminated by NULL.

Parameters:

  | argv | The command to execute should be the first element of this array, any additional ones are the command parameters and the array must be terminated with a NULL pointer.
  | flags | Must include either wxEXEC_ASYNC or wxEXEC_SYNC and can also include wxEXEC_NOHIDE, wxEXEC_MAKE_GROUP_LEADER (in either case) or wxEXEC_NODISABLE and wxEXEC_NOEVENTS or wxEXEC_BLOCK, which is equal to their combination, in wxEXEC_SYNC case.
  | callback | An optional pointer to wxProcess.
  | env | An optional pointer to additional parameters for the child process, such as its initial working directory and environment variables. This parameter is available in wxWidgets 2.9.2 and later only.

See also:
  wxShell(), wxProcess, External Program Execution Sample, wxLaunchDefaultApplication(), wxLaunchDefaultBrowser()

Include file:

    #include <wx/utils.h>

wxPerl Note: In wxPerl this function is called `Wx::ExecuteArgs`.

long wxExecute | ( | const wxString & | command,
|  | wxArrayString & | output,
|  | wxArrayString & | errors,
|  | int | flags = `0`,
|  | const wxExecuteEnv * | env = `NULL` |
| ) |  |  |

This is an overloaded version of wxExecute(const wxString&,int,wxProcess*), please see its documentation for general information.

This version adds the possibility to additionally capture the messages from standard error output in the errors array. As with the above overload capturing standard output only, execution is always synchronous.

Parameters:

  | command | The command to execute and any parameters to pass to it as a single string.
  | output | The string array where the stdout of the executed process is saved.
  | errors | The string array where the stderr of the executed process is saved.
  | flags | May include wxEXEC_NOHIDE, wxEXEC_MAKE_GROUP_LEADER (in either case) or wxEXEC_NODISABLE and wxEXEC_NOEVENTS or wxEXEC_BLOCK, which is equal to their combination. wxEXEC_SYNC is always implicitly added to the flags.
  | env | An optional pointer to additional parameters for the child process, such as its initial working directory and environment variables. This parameter is available in wxWidgets 2.9.2 and later only.

See also:
  wxShell(), wxProcess, External Program Execution Sample, wxLaunchDefaultApplication(), wxLaunchDefaultBrowser()

Include file:

    #include <wx/utils.h>

wxPerl Note: This function is called `Wx::ExecuteStdoutStderr:` it only takes the command argument, and returns a 3-element list (`status`, `output`, `errors`), where `output` and `errors` are array references.

void wxExit | ( |  | ) |

Exits application after calling wxApp::OnExit.

Should only be used in an emergency: normally the top-level frame should be deleted (after deleting all other frames) to terminate the application. See wxCloseEvent and wxApp.

Include file:

    #include <wx/app.h>

unsigned long wxGetProcessId | ( |  | ) |

Returns the number uniquely identifying the current process in the system.

If an error occurs, 0 is returned.

Include file:

    #include <wx/utils.h>

int wxKill | ( | long | pid,
|  | wxSignal | sig = `wxSIGTERM`,
|  | wxKillError * | rc = `NULL`,
|  | int | flags = `wxKILL_NOCHILDREN` |
| ) |  |  |

Equivalent to the Unix kill function: send the given signal sig to the process with PID pid.

The valid signal values are:

        enum wxSignal
        {
            wxSIGNONE = 0, // verify if the process exists under Unix
            wxSIGHUP,
            wxSIGINT,
            wxSIGQUIT,
            wxSIGILL,
            wxSIGTRAP,
            wxSIGABRT,
            wxSIGEMT,
            wxSIGFPE,
            wxSIGKILL,     // forcefully kill, dangerous!
            wxSIGBUS,
            wxSIGSEGV,
            wxSIGSYS,
            wxSIGPIPE,
            wxSIGALRM,
            wxSIGTERM      // terminate the process gently
        };

`wxSIGNONE`, `wxSIGKILL` and `wxSIGTERM` have the same meaning under both Unix and Windows but all the other signals are equivalent to `wxSIGTERM` under Windows.

Returns 0 on success, -1 on failure. If the rc parameter is not NULL, it will be filled with a value from the `wxKillError` enum:

        enum wxKillError
        {
            wxKILL_OK,            // no error
            wxKILL_BAD_SIGNAL,    // no such signal
            wxKILL_ACCESS_DENIED, // permission denied
            wxKILL_NO_PROCESS,    // no such process
            wxKILL_ERROR          // another, unspecified error
        };

The flags parameter can be wxKILL_NOCHILDREN (the default), or wxKILL_CHILDREN, in which case the child processes of this process will be killed too. Note that under Unix, for wxKILL_CHILDREN to work you should have created the process by passing wxEXEC_MAKE_GROUP_LEADER to wxExecute().

See also:
  wxProcess::Kill(), wxProcess::Exists(), External Program Execution Sample

Include file:

    #include <wx/utils.h>

bool wxShell | ( | const wxString & | command = `wxEmptyString` | ) |

Executes a command in an interactive shell window.

If no command is specified, then just the shell is spawned.

See also:
  wxExecute(), External Program Execution Sample

Include file:

    #include <wx/utils.h>

bool wxShutdown | ( | int | flags = `wxSHUTDOWN_POWEROFF` | ) |

This function shuts down or reboots the computer depending on the value of the flags.

Note:
  Note that performing the shutdown requires the corresponding access rights (superuser under Unix, SE_SHUTDOWN privilege under Windows NT) and that this function is only implemented under Unix and MSW.

Parameters:

  | flags | One of `wxSHUTDOWN_POWEROFF`, `wxSHUTDOWN_REBOOT` or `wxSHUTDOWN_LOGOFF` (currently implemented only for MSW) possibly combined with `wxSHUTDOWN_FORCE` which forces shutdown under MSW by forcefully terminating all the applications. As doing this can result in a data loss, this flag shouldn't be used unless really necessary.

Returns:
  true on success, false if an error occurred.

Include file:

    #include <wx/utils.h>

|  | [ top ]