PDA

View Full Version : IPC / reading stdin within non-GUI thread



mule
29th November 2007, 21:23
[ Qt 4.3.2 / Windows ]

I wish to pass (small amounts of) information from a "parent" process to spawned "child" processes.
To do this I am attempting to:
QProcess::write() text information from within the "parent" process to a "child" process's standard input.
QTextStream::read() text information from standard input within a "child" process.

To test this idea, I mocked up 2 applications, each with a button ... the "parent" application button action would perform the write, the "child" application button action would perform the read. This was SUCCESSFUL in transferring the information.

Because placing QTextStream::read() in a GUI thread will hang the GUI until there are bytes to read, I moved the "child" application logic to a background thread. NOW it does NOT work ... the "child" process never sees any bytes received.

I am thinking this is what is happening: Only a GUI thread can see standard input.
Am I correct? or should I keep looking for am implementation error on my part. (I have been looking and debugging a lot to arrive at this conclusion.)

I can supply code, but first I thought perhaps someone would know if my approach was even valid to pursue.

Thank y'all for any/all feedback.

wysota
29th November 2007, 21:28
Because placing QTextStream::read() in a GUI thread will hang the GUI until there are bytes to read, I moved the "child" application logic to a background thread. NOW it does NOT work ... the "child" process never sees any bytes received.

Use QIODevice::bytesAvailable() before calling read(). If it's 0, don't read at all - no need for other threads.

mule
29th November 2007, 22:01
I thought of that ... using QIODevice::bytesAvailable() was in an earlier attempt. (Actually, it was QTextStream::device()->bytesAvailable() that I tried.)
For that case, 0 was ALWAYS returned, so I did not pursue that any further. (Perhaps I had an error or misunderstanding with that attempt ... or, perhaps there was a buffering conflict ...)

[ It is dinner time here and I am being called home ... will return in the morning ... ]

[ Just realized I will be out-of-town all day tomorrow, so may be a while before I see posts ... ]

wysota
29th November 2007, 22:11
In that case connect to the readyRead signal of QProcess and read only when there is anything to be read.