Results 1 to 9 of 9

Thread: ¿How to stop thread that is capturing from a webcam?

  1. #1
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default ¿How to stop thread that is capturing from a webcam?

    Hello everybody.

    I have a little problem.

    I have an app with two buttons, one for start an image capturing thread from a webcam and show them, and the another one is for stop thread (wich means stop capturing).
    My thread's run method look like this:

    Qt Code:
    1. void thread::run()
    2. {
    3.  
    4. while(1)
    5. {
    6. webcam_read_frame(parent_thread->cam_fd, parent_thread->pixelImagen);
    7.  
    8. if(parent_thread->need_update)
    9. {
    10. QCustomEvent *evento = new QCustomEvent(QEvent::User);
    11. parent_thread->need_update = false;
    12. QApplication::postEvent(parent_thread, evento);
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    The idea is the thread keep running 'till i push "stop" button.



    The code for my stop buttons is:

    Qt Code:
    1. void gui_tracksys::slot_pushButtonStop_clicked()
    2. {
    3. webcam_close(cam_fd); //this line stops capture and close webcam
    4.  
    5. //also delete other things like QImage, QPixmap, QPainter, etc...
    6.  
    7. if (myThread->running())
    8. {
    9. myThread->terminate();
    10. myThread->wait(5000);
    11. delete myThread;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 


    I see the problem is I keep calling webcam_read_frame() after I've called webcam_close(), because when i click "stop" the first thing i do is close webcam and thread still running, so the run method try to call webcam_read_frame() but cam doesn't exist anymore.
    I've tried to stop thread first and then close webcam, but this cause my app becomes unresponsive and need to click "x" (window close button) and then click "force close" when asked.

    I don't know how to fix my problem, so any suggestion is welcome.

    I'm using QT3/c/c++

    I'm sorry about my bad english... Thank you very much...

    PS:If need more info about my code i will post, just let me know.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ¿How to stop thread that is capturing from a webcam?

    Try this:
    Qt Code:
    1. void thread::run()
    2. {
    3.  
    4. while( _go )
    5. {
    6. ...
    7. }
    8. // clean up
    9. }
    10.  
    11. void thread::stop() { _go = false; }
    12.  
    13. ...
    14.  
    15. if (myThread->running())
    16. {
    17. myThread->stop();
    18. myThread->wait(5000);
    19. delete myThread;
    20. }
    21. ...
    To copy to clipboard, switch view to plain text mode 
    This way the thread will be able to finish gently and clean up after itself.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 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 thread that is capturing from a webcam?

    Why not stop the thread first and then close the camera?

    Apart from that try to avoid using terminate(). Instead raise some flag in the thread (so that your while(1) loop becomes "while(!finish)") and call QThread::wait() afterwards (in the main thread) to pause until the worker thread exits. You shouldn't be experiencing those weird effects then.

  4. #4
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: ¿How to stop thread that is capturing from a webcam?

    I've tried as you suggested but i get an error:

    Qt Code:
    1. Class "thread" has no member named "stop"
    To copy to clipboard, switch view to plain text mode 

    I suppose the cause is i am using QT3, and there is no a member function named "stop" in the QThread class reference (http://doc.trolltech.com/3.3/qthread.html).

    I change this line to terminate(); and i get less error than before (I forgot to mention i got 4 errors, all the same, one for each buffer i use to queue frames), but still getting 1 error because cam does not exist and thread still reading frames.

    this is the way i do:

    Qt Code:
    1. void thread::run()
    2. {
    3.  
    4. while(padre->capturing)
    5. {
    6. webcam_read_frame(parent->cam_fd, parent->pixelImagen);
    7. if(padre->need_update)
    8. {
    9. QCustomEvent *evento = new QCustomEvent(QEvent::User);
    10. padre->need_update = false;
    11. QApplication::postEvent(parent, evento);
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    and stop button:

    Qt Code:
    1. void gui_tracksys::slot_pushButtonStop_clicked()
    2. {
    3. captururing = false;
    4.  
    5. webcam_close(cam_fd);
    6.  
    7. if (miHilo->running())
    8. {
    9. miHilo->terminate();
    10. miHilo->wait(5000);
    11. delete miHilo;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    What can i do?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ¿How to stop thread that is capturing from a webcam?

    Quote Originally Posted by JoseTlaseca View Post
    I suppose the cause is i am using QT3, and there is no a member function named "stop" in the QThread class reference
    You were supposed to add it to your class definition.

    Quote Originally Posted by JoseTlaseca View Post
    I change this line to terminate();
    Forget about this method.

    Quote Originally Posted by JoseTlaseca View Post
    webcam_close(cam_fd);
    This should be in run(). If the thread uses the webcam, it should open it and it should clean it up. Otherwise you will have webcam code smeared all over your sources.

  6. #6
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: ¿How to stop thread that is capturing from a webcam?

    Wysota says:
    Qt Code:
    1. Why not stop the thread first and then close the camera?
    To copy to clipboard, switch view to plain text mode 

    If I stop thread first and the close cam i get a segmentation fault and program crash.
    So i close cam first and then stop thread, this way i get error but program finish correctly.

  7. #7
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: ¿How to stop thread that is capturing from a webcam?

    Quote Originally Posted by jacek View Post
    You were supposed to add it to your class definition.
    I see... ok, but what code i need to put inside that "myThread->stop();"???

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 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 thread that is capturing from a webcam?

    See line #11 of Jacek's code.

  9. #9
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: ¿How to stop thread that is capturing from a webcam?

    Hello!

    Finally i fixed that problem.

    I was confused with your advices but now i'm clear and everythings ok.

    The final code of my "Stop" button is:

    Qt Code:
    1. void gui_tracksys::slot_pushButtonStop_clicked()
    2. {
    3. if (myThread->running())
    4. {
    5. myThread->stop();
    6. myThread->wait(5000);
    7. delete myThread;
    8. }
    9.  
    10. webcam_close(cam_fd);
    11. }
    To copy to clipboard, switch view to plain text mode 

    and the thread's code is:

    Qt Code:
    1. void thread::run()
    2. {
    3. while(parent->capturing)
    4. {
    5. webcam_read_frame(parent->cam_fd, parent->pixelImagen);
    6.  
    7. if(parent->need_update)
    8. {
    9. QCustomEvent *evento = new QCustomEvent(QEvent::User);
    10. parent->need_update = false;
    11. QApplication::postEvent(parent, evento);
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    And the thread's "stop" method:

    Qt Code:
    1. void thread::stop()
    2. {
    3. parent->capturing = false;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Finally in class destructor i delete all pointers used.

    Thank you very much.

    problem SOLVED...

Similar Threads

  1. Qthreads and webcam app
    By Schizo in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2007, 09:17
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. Replies: 10
    Last Post: 20th March 2007, 22:19

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.