How to read live output from another process ?

Vinod K Chandran kcvinu82 at gmail.com
Thu Jun 29 05:02:30 UTC 2023


On Friday, 23 June 2023 at 23:37:29 UTC, Vinod K Chandran wrote:
> Hi all,

Hi, I found the solution by myself. We can use Pipe struct for 
this job.
Here is the code looks like. This is for future readers.

```d
void onBtnBurnClick(Control c, EventArgs e) { // A button click 
event handler
	if (!jobStarted) {
		jobStarted = true;
		btnBurn.text = "Stop the job";
		string cmd = makeCommand();
		ff = spawn(&runInAnotherThread, cmd); // ff is global var
	} else {
		ff.send(-1); // Send a signal to stop the job
		btnBurn.text = "Start the job";
	}
}

void runInAnotherThread(string cmd) {
	Duration du = dur!"hnsecs"(-2); // Minus value for no waiting
	auto psin = pipe();
	auto psout = pipe();
	auto pserr = pipe();
	spawnShell(cmd, psin.readEnd, psout.writeEnd, pserr.writeEnd , 
null, Config.detached);
	string line;
     while ((line = pserr.readEnd.readln()) !is null){
		receiveTimeout(du, (int dummy) {psin.writeEnd.writeln("q"); 
psin.writeEnd.flush(); }); // Here, entering "q" is app specific 
command to stop.
		writefln("pipe err: %s", line);
		stdout.flush;
	}
}
```
By this way, you can asynchronously process the output from the 
child process.


More information about the Digitalmars-d-learn mailing list