AsyncProcess

AsyncProcess is a non blocking process that exposes two assignable events allowing to be notified when the process output has changed or when the process has terminated.

This class relies on an internal ThreadTimer that could not work in all the contexts (for example in a X window).

Constructors

this
this()

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Members

Functions

execute
void execute()

Executes and returns immediatly.

onOutputBuffer
void onOutputBuffer(void delegate(Object) value)
void delegate(Object) onOutputBuffer()

Sets or gets the event called when the process has availalbe output.

onTerminate
void onTerminate(void delegate(Object) value)
void delegate(Object) onTerminate()

Sets or gets the event called when the process terminates.

readOutput
bool readOutput(ref T[] t)

Reads from the output buffer

Inherited Members

From Process

internalExec
void internalExec()

default, non blocking, execute

execute
void execute()

Executes the process and returns when it terminates.

executeAndWrite
void executeAndWrite(auto ref T t)

Executes then writes and finally closes the input stream.

executeAndPipeFile
void executeAndPipeFile(const(char[]) filename)

Executes, fills the input stream with a file and finally closes the input.

terminate
void terminate(int status = 0)

Forces the process termination.

usePipes
void usePipes(bool value)
bool usePipes()

Sets or gets if the process streams are redirected.

errorToOutput
void errorToOutput(bool value)
bool errorToOutput()

Sets or gets if the error stream is redirected to the output stream. Only applied if usePipes is set to true.

newEnvironment
void newEnvironment(bool value)
bool newEnvironment()

Sets or gets if the value passed in environment fully replaces the default environment.

executable
void executable(const(char)[] value)
const(char)[] executable()

Sets or gets the process executable.

workingDirectory
void workingDirectory(const(char)[] value)
const(char)[] workingDirectory()

Sets or gets the process working directory.

environmentAA
string[string] environmentAA()

The environment, as an associative array.

environment
void environment(const(char)[] value)
const(char)[] environment()

Sets or gets the environment.

fullEnvironment
string fullEnvironment()

Returns the full environment.

parameters
void parameters(string value)
string parameters()

Sets or gets the parameters for this process.

hideConsole
void hideConsole(bool value)
bool hideConsole()

Sets or gets if the console of the program is visible.

isShellCommand
void isShellCommand(bool value)
bool isShellCommand()

Sets or gets if the executable field actually represents a shell command.

waitFor
int waitFor()

Waits for the process and only returns when it terminates.

terminated
bool terminated()

Indicates wether the process is terminated.

exitStatus
int exitStatus()

Indicates the process exit status.

input
File input()

The process input stream.

output
File output()

The process output stream.

error
File error()

The process error stream.

Examples

1 import std.process: environment;
2 if (environment.get("TRAVIS") == "true")
3     return;
4 
5 import std.file: write, exists, tempDir, getcwd, remove;
6 import std.path: dirSeparator;
7 
8 auto code =
9 q{
10     import std.stdio;
11     import core.thread;
12     void main()
13     {
14         write("hello world");
15         stdout.flush;
16         Thread.sleep(dur!"msecs"(15));
17         Thread.sleep(dur!"msecs"(15));
18     }
19 };
20 
21 version(Windows) enum exeExt = ".exe"; else enum exeExt = "";
22 version(Windows) enum objExt = ".obj"; else enum objExt = ".o";
23 
24 string fname = getcwd ~ dirSeparator ~ "TempSource";
25 string srcName = fname ~ ".d";
26 string exeName = fname ~ exeExt;
27 string objName = fname ~ objExt;
28 scope(exit)
29 {
30     remove(srcName);
31     remove(exeName);
32     remove(objName);
33 }
34 
35 write(srcName, code);
36 
37 // compiles code with dmd
38 Process dmdProc = construct!Process;
39 dmdProc.executable = "dmd";
40 dmdProc.parameters = srcName;
41 dmdProc.execute;
42 assert(dmdProc.terminated);
43 assert(dmdProc.exitStatus == 0);
44 assert(exeName.exists);
45 
46 // aggregate to catch the events
47 struct Catcher
48 {
49     bool ter;
50     bool flg;
51     void bufferAvailable(Object notifier)
52     {
53         flg = true;
54         AsyncProcess proc = cast(AsyncProcess) notifier;
55         //TODO-cleaks: this readln creates a leak
56         string s = proc.output.readln;
57         assert(s == "hello world");
58     }
59     void terminate(Object notifier)
60     {
61         ter = true;
62     }
63 }
64 Catcher catcher;
65 
66 // run produced program
67 AsyncProcess runProc = construct!AsyncProcess;
68 runProc.executable = exeName;
69 runProc.usePipes = true;
70 runProc.onOutputBuffer = &catcher.bufferAvailable;
71 runProc.onTerminate = &catcher.terminate;
72 assert(runProc.onOutputBuffer);
73 assert(runProc.onTerminate);
74 runProc.execute;
75 import core.thread;
76 while (!catcher.ter) {Thread.sleep(dur!"msecs"(10));}
77 assert(runProc.terminated);
78 assert(runProc.exitStatus == 0);
79 assert(catcher.ter);
80 assert(catcher.flg);
81 
82 destructEach(runProc, dmdProc);

Meta