final class · System.Diagnostics
Process
public final class Process
A program started by this one.
- Tipo
- final class
- Espacio de nombres
- System.Diagnostics
- Ensamblado
- System.Core
- Acceso
- public, final
- Archivo fuente
Process.ant
Herencia
- Clases base
-
AnyProcess
Constructores
Propiedades
HandleStream Output { get; }
#
What the program writes to standard output (reads block until it writes or ends).
Métodos
Runs a command line through the system's shell, sharing this program's console: its exit code.
Starts command with arguments; its standard output is Output.
static Run(str command, str arguments) : ProcessResult
#
Runs command to the end: its exit code and everything it wrote to standard output.
ReadAll() : str
#
All the output until the program closes it, as text.
Waits for the program to end: its exit code (the output stream is closed with it).
Miembros privados e internos (7)
Miembros heredados (5)
GetTypemétodo de AnyUndescribedmétodo de AnyToStringmétodo de AnyHashmétodo de Anydestroymétodo de Any
Código fuente
Process en Antril, descompilado a partir de su Larva HIR.
// System.Diagnostics.Process — System.Core (decompiled from Larva HIR)
import System;
import System.IO;
into namespace System.Diagnostics
{
/// A program started by this one.
public final class Process
{
i64 handle;
HandleStream output;
i32 exitCode;
bool exited;
static extern("proc.start") StartNative(u16[] command, u16[] arguments) : i64;
static extern("proc.wait") WaitNative(i64 handle) : i64;
static extern("process.run") RunCommand(u16[] command) : i64;
public:
new(i64 handle)
{
this.handle = handle;
output = new HandleStream(handle, "process", true, false);
exitCode = -1;
}
/// Runs a command line through the system's shell, sharing this program's console: its exit code.
static Run(str command) : i32 => (i32)RunCommand(Unicode.ToUtf16(command));
/// Starts `command` with `arguments`; its standard output is `Output`.
static Start(str command, str arguments) : Process
{
i64 handle = StartNative(Unicode.ToUtf16(command), Unicode.ToUtf16(arguments));
if (handle < 0)
throw new IOError("cannot start " + command);
return new Process(handle);
}
/// Runs `command` to the end: its exit code and everything it wrote to standard output.
static Run(str command, str arguments) : ProcessResult
{
Process process = Start(command, arguments);
str output = process.ReadAll();
return new ProcessResult(process.WaitForExit(), output);
}
/// All the output until the program closes it, as text.
ReadAll() : str
{
StringBuilder text = new StringBuilder();
u8[] buffer = new u8[4096];
loop (true)
{
i32 n = output.Read(buffer, 0, 4096);
if (n <= 0)
break;
u8[] got = new u8[n];
Array.Copy(buffer, 0, got, 0, n);
text.Append(new str(got));
}
return text.ToString();
}
/// Waits for the program to end: its exit code (the output stream is closed with it).
WaitForExit() : i32
{
if (!exited)
{
exitCode = (i32)WaitNative(handle);
exited = true;
}
return exitCode;
}
/// What the program writes to standard output (reads block until it writes or ends).
HandleStream Output
{
get => output;
}
i32 ExitCode
{
get => WaitForExit();
}
}
}