Qt-interest Archive, September 2007
moveToThread and startTimer confusion
Message 1 in thread
Hey all,
I'm having a problem with QThreads that I just can't seem to solve. I'm
trying to use the startTimer(0) function to repeatedly execute a network
capture. I have found a solution by starting the thread using:
void FbNetworkInQt::startThread()
{
startTimer( 0 );
start();
moveToThread( this );
}
The problem is that I cannot stop the thread, change the network settings,
and start it again. Since the timer still belongs to the stopped thread. I
have tried changing ownership from the exec function:
void FbNetworkInQt::run()
{
exec();
stopTimer();
moveToThread( QCoreApplication::instance()->thread() );
}
but this just causes more problems... and I'm not sure that this thread
should return to the core application since it may have been started by
another thread.
I'm working on a way to update the network data without stopping the thread,
but I would like to know how to start and stop threads without a problem.
Any assistance would be appreciated,
Brian!
Message 2 in thread
On 9/4/07, Brian McKinnon <bpmckinnon@xxxxxxxxx> wrote:
> I'm having a problem with QThreads that I just can't seem to solve. I'm
> trying to use the startTimer(0) function to repeatedly execute a network
> capture. I have found a solution by starting the thread using:
[snip]
> I'm working on a way to update the network data without stopping the thread,
> but I would like to know how to start and stop threads without a problem.
>
> Any assistance would be appreciated,
Sub-class QThread and place a call to exec() in the implemented run() method.
Move the instance of your "worker" object to the thread and connect
the started()
and finished() signals to slots in the worker object to do your work
(start and stop timers, perform network I/O).
Jens
--
[ signature omitted ]
Message 3 in thread
On 9/4/07, Jens Luedicke <jens.luedicke@xxxxxxxxx> wrote:
>
> On 9/4/07, Brian McKinnon <bpmckinnon@xxxxxxxxx> wrote:
>
> > I'm having a problem with QThreads that I just can't seem to solve. I'm
> > trying to use the startTimer(0) function to repeatedly execute a network
> > capture. I have found a solution by starting the thread using:
>
> [snip]
>
> > I'm working on a way to update the network data without stopping the
> thread,
> > but I would like to know how to start and stop threads without a
> problem.
> >
> > Any assistance would be appreciated,
>
> Sub-class QThread and place a call to exec() in the implemented run()
> method.
> Move the instance of your "worker" object to the thread and connect
> the started()
> and finished() signals to slots in the worker object to do your work
> (start and stop timers, perform network I/O).
>
>
> Jens
>
> --
> Jens Luedicke
> web: http://perldude.de/
I got it to work... it turns out my network thread was no longer timing out
and the thread was being killed before exec completed. I implemented the
started/finished handlers though and it works great. Cleaned up my code
too!
For anyone else trying to do the timer thread stuff (since it took me a
while to figure it out), this is the code I ended up with.
void FbNetworkInQt::handleStarted()
{
_timer = startTimer( 0 );
_startThread = thread();
moveToThread( this );
}
void FbNetworkInQt::run()
{
exec();
}
void FbNetworkInQt::handleFinished()
{
killTimer( _timer );
moveToThread( _startThread );
}
Where _timer is an int, and _startThread is a QThread *. I think the
_startThread makes sense, as that puts the object back into the original
thread. If anyone sees anything totally wrong with this let me know.
Also, is it possible to use automatic connections in this object? I tried:
void on_started(), and
void on_this_started()
and neither worked.
Thanks Jens,
Brian!
--
[ signature omitted ]
Message 4 in thread
Jens Luedicke wrote:
> Sub-class QThread and place a call to exec() in the implemented run() method.
> Move the instance of your "worker" object to the thread and connect
> the started()
> and finished() signals to slots in the worker object to do your work
> (start and stop timers, perform network I/O).
>
Some additional publicity for
http://labs.trolltech.com/blogs/2007/07/05/qthreads-no-longer-abstract/.
The link has an excellent producer/consumer example. In short, in 4.4,
there will be no need to subclass QThread.
QThread thread;
// MyWorker has a slot doWork() that uses startTimer()
MyWorker worker;
worker.moveToThread(&thread);
worker.connect(&thread, SIGNAL(started()), SLOT(doWork()));
thread.start();
...
thread.quit();
Girish
--
[ signature omitted ]
Message 5 in thread
On 9/6/07, Girish Ramakrishnan <girish@xxxxxxxxxxxxx> wrote:
> Some additional publicity for
> http://labs.trolltech.com/blogs/2007/07/05/qthreads-no-longer-abstract/.
> The link has an excellent producer/consumer example. In short, in 4.4,
> there will be no need to subclass QThread.
>
> QThread thread;
> // MyWorker has a slot doWork() that uses startTimer()
> MyWorker worker;
> worker.moveToThread(&thread);
> worker.connect(&thread, SIGNAL(started()), SLOT(doWork()));
> thread.start();
> ...
> thread.quit();
Yes, that I what I used for my own implementation.
Another thing:
What is the preferred way to exit a thread from within
the worker object? thread()->exit(-1) works, but it would exit the whole
application if the object isn't part of a Non-GUI thread.
Jens
--
[ signature omitted ]
Message 6 in thread
On Thursday 06 September 2007 09:24:17 Jens Luedicke wrote:
> Another thing:
>
> What is the preferred way to exit a thread from within
> the worker object? thread()->exit(-1) works, but it would exit the whole
> application if the object isn't part of a Non-GUI thread.
I like to put a done() or finished() signal in the worker, and connect that to
QThread::quit() ;)
--
[ signature omitted ]