Results 1 to 5 of 5

Thread: how to stop one thread till other thread get finished

  1. #1
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to stop one thread till other thread get finished

    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:n_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.
    Last edited by vani.pv; 20th September 2012 at 13:08.

  2. #2
    Join Date
    Jun 2010
    Location
    Pretoria, South Africa
    Posts
    22
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to stop one thread till other thread get finished

    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?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to stop one thread till other thread get finished

    Qt Code:
    1. QThread *thread = ...;
    2. connect(thread, SIGNAL(finished()), &loop, SLOT(quit()));
    3. thread->start();
    4. loop.exec();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to stop one thread till other thread get finished

    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.
    Last edited by vani.pv; 24th September 2012 at 04:41.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to stop one thread till other thread get finished

    There is nothing to elaborate. Start reading the docs. Using sockets does not require any threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. want to get thread to stop when application exits
    By dan146 in forum Qt Programming
    Replies: 6
    Last Post: 2nd May 2012, 23:07
  2. Wait in thread till QTcp socket have some thing to read
    By hasnain in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2010, 12:46
  3. get finished signal from a thread
    By ProTonS in forum Qt Programming
    Replies: 4
    Last Post: 21st August 2009, 14:17
  4. ¿How to stop thread that is capturing from a webcam?
    By JoseTlaseca in forum Qt Programming
    Replies: 8
    Last Post: 28th August 2008, 04:45
  5. Stop the thread
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 10:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.