PDA

View Full Version : signals and events queued...



soul_rebel
11th April 2006, 11:56
i am reading output from a process and printing it in a textedit, but the text is printed in big blocks every once in while instead of being added line-by-line continuously.

a model of this operation would look like this:

1. obect1(qthread) posts event with on line to base object
2. base object receives event and emits string as a signal
3. slot in gui object receives signal and appends the passed string to the textedit

i tried using QApplication::sendPostedEvents(); every three lines in object1(the thread) but that locks up the thread.
then i tried using kapp->processEvents(); in the gui thread everytime the slot receives a line (because i thought that maybe only the redraws of the textedit are queued), but then the whole app freezes :(
any ideas?

thanks!

jacek
11th April 2006, 15:06
i am reading output from a process
What do you mean by "process"? An external program or some activity within your application?

soul_rebel
11th April 2006, 15:30
What do you mean by "process"? it is a unix io-pipe that runs a command that produces output ;)

jacek
11th April 2006, 15:35
it is a unix io-pipe that runs a command that produces output ;)
Then maybe the data is buffered by this pipe? How often does that thread post events?

soul_rebel
11th April 2006, 16:44
the thread posts an event with a single line as soon as that is available, which in my case means it posts events nearly all the time.

jacek
11th April 2006, 17:14
the thread posts an event with a single line as soon as that is available, which in my case means it posts events nearly all the time.
Have you checked that? Maybe it posts those events in bursts?

soul_rebel
12th May 2006, 11:00
i am using kprocio instead of the unix io-pipe now and it is even worse.... i think i will just post some code:
this is the relevant part of object 1

activeProcs.append(proc.name());

proc.start(KProcess::OwnGroup, KProcess::All); /// ownGroup so children spawned by proc can be killed too + comm=alloutput

/// to prevent queueing of messages we start a timer
QTimer *timer = new QTimer(this);
connect( timer, SIGNAL(timeout()), this, SLOT(postAllEvents()) );

timer->start(2*1000);

while (proc.isRunning() || (proc.readln(Buffer) != -1))
{
/// the output will not be processed/interpreted by this call, but instead
/// by the gui thread, because that has relevant information neede


if (Buffer!="")
{
Buffer.remove('\n');
Buffer.remove(QChar(13));
qDebug("posting 1 event");
QApplication::postEvent(this, new QCustomEvent((QEvent::Type)1200, new QString(Buffer))); /// signal it out
}


if (toBeStoppedProcs.contains(proc.name()) || toBeStoppedProcs.contains("*")) /// should be interrupted
{
proc.kill(SIGINT); /// try to terminate nicely
if (!proc.wait(2))
proc.kill(SIGKILL);/// then we kill it

Buffer = "--> Process interrupted by User."; /// get line from output and make it red
QApplication::postEvent(this, new QCustomEvent((QEvent::Type)1201, new QString(Buffer))); /// signal it out
}

Buffer = "";
}

timer->stop();
delete timer;

/// erase it from both lists
activeProcs.erase(activeProcs.find(proc.name()));
while (toBeStoppedProcs.contains(proc.name()))
toBeStoppedProcs.erase(toBeStoppedProcs.find(proc. name()));

QApplication::postEvent(this, new QCustomEvent((QEvent::Type)1202, NULL)); /// signal it out
i have changed bahviuor so that a timer in the thread sendsallpostedevents every 2 seconds and i have a second timer in the gui class that calls processEvents() every 2 seconds.
they are succesfully called (i have a qdebug message that tells me "sending events" and "processing events"every two seconds), but "posting one event" appears only in bursts often even only after the thread finished....
what can i do about this? i thought sendpostedevents and processevents should prevent all qt-related event-queueing.
thanks for you help

jacek
12th May 2006, 11:30
while (proc.isRunning() || (proc.readln(Buffer) != -1))
If the process is running, you will enter the loop without reading its output.

soul_rebel
12th May 2006, 13:32
thanks, i just realised that :o
i thought || was and/or with all arguments being checked....