Struct CreateProcessW::Child

source ·
pub struct Child { /* private fields */ }
Expand description

Representation of a running or exited child process.

This structure is used to represent and manage child processes. A child process is created via the Command struct, which configures the spawning process and can itself be constructed using a builder-style interface.

§Warnings

Calling wait is necessary for the OS to release resources. A process that terminated but has not been waited on is still around as a “zombie”. Leaving too many zombies around may exhaust global resources.

This library does not automatically wait on child processes (not even if the Child is dropped), it is up to the application developer to do so. As a consequence, dropping Child handles without waiting on them first is not recommended in long-running applications.

§Examples

use CreateProcessW::Command;

let mut child = Command::new("notepad.exe")
    .spawn()
    .expect("failed to execute child");

let status = child.wait().expect("failed to wait on child");

assert!(status.success());

Implementations§

source§

impl Child

source

pub fn kill(&self) -> Result<(), Error>

Forces the child process to exit. If the child has already exited, a KillFailed error is returned.

This function is used to unconditionally cause a process to exit and stops execution of all threads within the process and requests cancellation of all pending I/O. The terminated process cannot exit until all pending I/O has been completed and canceled. When a process terminates, its kernel object is not destroyed until all processes that have open handles to the process have released those handles.

Equivalent to the TerminateProcess function. Note that the value passed as the uExitCode is always 0 at the moment.

§Examples
use CreateProcessW::Command;

let mut command = Command::new("notepad.exe");

if let Ok(mut child) = command.spawn() {
    child.kill().expect("notepad wasn't running");
} else {
    println!("notepad didn't start");
}
source

pub fn wait(&self) -> Result<ExitStatus, Error>

Waits for the child to exit completely, returning the status that it exited with and closing handles. This function will continue to have the same return value after it has been called at least once.

If the function fail, it return a GetExitCodeFailed error.

This is equivalent to calling the WaitForSingleObject][wait-for-single-object] and the [CloseHandle` functions.

§Examples
use CreateProcessW::Command;

let mut command = Command::new("notepad.exe");

if let Ok(mut child) = command.spawn() {
    child.wait().expect("command wasn't running");
    println!("Child has finished its execution!");
} else {
    println!("notepad didn't start");
}
source

pub fn try_wait(&self) -> Result<Option<ExitStatus>, Error>

Attempts to collect the exit status of the child if it has already exited.

This function will not block the calling thread and will only check to see if the child process has exited or not.

If the child has exited, then Ok(Some(status)) is returned. If the exit status is not available at this time then Ok(None) is returned. If an error occurs, then that error is returned.

Equivalent to the [GetExitCodeProcess][get-exit-code-process] function.

Note that this function will call CloseHandle if the child has exited. If the function fail, a GetExitCodeProcess error is returned.

§Examples
use CreateProcessW::Command;

let mut child = Command::new("notepad.exe").spawn().unwrap();

match child.try_wait() {
    Ok(Some(status)) => println!("exited with: {}", status.code()),
    Ok(None) => {
        println!("status not ready yet, let's really wait");
        let status = child.wait().expect("cannot wait process");
        println!("waited: {}", status.code());
    }
    Err(e) => println!("error attempting to wait: {}", e),
}
source

pub fn id(&self) -> u32

Returns the process identifier associated with this child.

§Examples
use CreateProcessW::Command;

let mut command = Command::new("notepad.exe");

if let Ok(child) = command.spawn() {
    println!("Child's ID is {}", child.id());
} else {
    println!("notepad didn't start");
}

Trait Implementations§

source§

impl Debug for Child

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Child

§

impl RefUnwindSafe for Child

§

impl Send for Child

§

impl Sync for Child

§

impl Unpin for Child

§

impl UnwindSafe for Child

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.