PDA

View Full Version : how to stop one thread till other thread get finished



vani.pv
20th September 2012, 13:08
Hi.

I have two threads in my application.i am using them in several functions.But in one function i do calculation-1 then call thread-1.I need to wait till thread-1 get finished then i need to do calculation-2 and start thread -2.
But i dont know how to stop the process after calling the thread-1.
example:

void onstartbutton()
{

....................
.............
............... i am sending data to socket
thread1->start();
.............
............
............i am sending data to same socket
thread2->start()
}
after starting the thread one i need to wait till it get finished.I have used
while(!thread->isrunning());

but my gui is getting blocked.

I thought to use thread finished signal like this
connect(thread1,SIGNAL(finished()),this,SLOT(onfin ished1()));
but no use.
and while debugging i came to know that i the socket operations after the thread-1 not working .

can any one please tell me how to fix it.........


Thanks

Added after 34 minutes:

well here is my code for reference.



mainform.cpp

void MainForm::on_pstartBtn_clicked()
{

wbufstr = "PP0";
bawbuf = wbufstr.toUtf8();
bytsentv = pedsock->write(bawbuf,bawbuf.length());
pedsock->waitForBytesWritten(2000);
if(bytsentv != bawbuf.length())
QMessageBox::warning(this,"Antenna Control Unit","Failed to Send PP command",0,1);
memset(mreadbuf,'\0',40);

panthread->start()

wbufstr = "TP0";
bawbuf = wbufstr.toUtf8();
bytsentv = pedsock->write(bawbuf,bawbuf.length());
pedsock->waitForBytesWritten(2000);
if(bytsentv != bawbuf.length())
QMessageBox::warning(this,"Antenna Control Unit","Failed to Send TP command",0,1);
memset(mreadbuf,'\0',40);

tiltthread->start();
}


panthread.cpp

void panthread::run()
{
this->PanStop = false;
for(; ; )
{
if(this->PanStop)
break;
emit PanReadAngle();
this->msleep(100);
}
}

tiltthread.cpp

void tiltthread::run()
{
this->tiltStop = false;
for(; ; )
{
if(this->tiltStop)
break;
emit tiltReadAngle();
this->msleep(100);
}
}

here panreadangle (), and tiltreadangle() are signals connected to panpos in my main form.


i cand send both commands to my pedestal which is interfaced through ethernet.so i need to wait untiol panthread is finished.then i need to send other commands to pedestal.

how can i stop the tiltthread till panthread get finished.

Robbie
21st September 2012, 12:48
Why do you need to split your processing into two separate threads?

Why don't you just put everything in n_pstartBtn_clicked() into the same thread?

wysota
21st September 2012, 13:50
QThread *thread = ...;
QEventLoop loop;
connect(thread, SIGNAL(finished()), &loop, SLOT(quit()));
thread->start();
loop.exec();

vani.pv
24th September 2012, 04:41
Thanks wysota.
But i int get u.can you please elaborate...

Added after 11 minutes:

I am beginner to qt and dont how to do it in a single thread.if u have an idea please let me know,so that it will b helpful for me.

wysota
24th September 2012, 07:28
There is nothing to elaborate. Start reading the docs. Using sockets does not require any threads.