How can I get notified when an process created by spawnShell() has exit?

Basile B. b2.temp at gmx.com
Wed Mar 7 20:13:00 UTC 2018


On Wednesday, 7 March 2018 at 15:03:28 UTC, Marc wrote:
> I do need to start (up to 4 a time) processes in parallel but 
> I'd like to get notified (similar to C#'s Process.Exited Event) 
> when the process exits. How can I do that in D?

You can use pipeShell and a control loop to check when the 
ProcessPipes it returns is done.

example:

```d
void main()
{
     import std.stdio, std.process, std.conv, std.random, 
core.thread,
         std.algorithm, std.range;

     ProcessPipes[4] pp;
     iota(0,4).each!(a => pp[a] = pipeShell("sleep " ~ 
uniform(1,20).to!string));
     while (pp[].any!(a => !tryWait(a.pid).terminated))
         Thread.sleep(dur!"msecs"(50));

     writeln("done");
}
```

You can wrap in a struct to hide the ugly bits.


More information about the Digitalmars-d-learn mailing list