How to exit main process when sub thread exit abnormally



  • Hi,
    I am using QCoreAppication in my main to start a event loop(this can be seen as Main process?), and I create a thread to deal with captures from uvc camera. Here is the problem: During running, I pull out the camera, the imshow window of opencv will be closed, but the Main Process still can be seen background(ps -e | grep MyAPP), even I add "_Exit / exit / abort" ,the Main process still there and can't be closed.

    Does anyone know how to deal with this?thanks very much


  • Moderators

    @LengJianHanShuang said in How to exit main process when sub thread exit abnormally:

    does there any api of qt exist that I can use to deal with the disconnect of camera?

    It sounds like you are using OpenCV to read your camera? Use the same OpenCV API to detect the disconnection.

    It would be even better if your app doesn't quit when the camera is disconnected; instead, see if you can wait for the camera to re-connect again and re-open the imshow window.

    And then even if I detect the disconnect of camera, I can exit current thread, but how should I do to tell the main process to exit?

    QCoreApplication::quit() stops the main event loop.

    If your used a QThread to run your second thread, connect the QThread::finished() signal to the QCoreApplication::quit() slot.


  • Moderators

    @LengJianHanShuang said in How to exit main process when sub thread exit abnormally:

    I pull out the camera

    Do not let the thread exit abnormally. Write code to stop the thread normally when your program detects that the camera has been disconnected.



  • @JKSH
    Thanks for your advice, I am new to Qt, does there any api of qt exist that I can use to deal with the disconnect of camera?
    And then even if I detect the disconnect of camera, I can exit current thread, but how should I do to tell the main process to exit?


  • Moderators

    @LengJianHanShuang said in How to exit main process when sub thread exit abnormally:

    does there any api of qt exist that I can use to deal with the disconnect of camera?

    It sounds like you are using OpenCV to read your camera? Use the same OpenCV API to detect the disconnection.

    It would be even better if your app doesn't quit when the camera is disconnected; instead, see if you can wait for the camera to re-connect again and re-open the imshow window.

    And then even if I detect the disconnect of camera, I can exit current thread, but how should I do to tell the main process to exit?

    QCoreApplication::quit() stops the main event loop.

    If your used a QThread to run your second thread, connect the QThread::finished() signal to the QCoreApplication::quit() slot.



  • @JKSH said in How to exit main process when sub thread exit abnormally:

    @LengJianHanShuang said in How to exit main process when sub thread exit abnormally:

    does there any api of qt exist that I can use to deal with the disconnect of camera?

    It sounds like you are using OpenCV to read your camera? Use the same OpenCV API to detect the disconnection.
    I am sorry for my late reply! yes, you are right, I am using opencv to read camera, but when I disconnect the camera, there is segmentation fault in my app, and most times it will exit abnormally. I don't know which opencv API can be used to listen the disconnection of camera, so I didn't deal with the event that camera has disconnected.

    It would be even better if your app doesn't quit when the camera is disconnected; instead, see if you can wait for the camera to re-connect again and re-open the imshow window.

    And then even if I detect the disconnect of camera, I can exit current thread, but how should I do to tell the main process to exit?

    QCoreApplication::quit() stops the main event loop.

    If your used a QThread to run your second thread, connect the QThread::finished() signal to the QCoreApplication::quit() slot.
    Thanks very much for your advice, it seems worked for me, I will give it a try.

    I was in business trip that time, I was able to restart the app with shell when it exit abnormally, It is a good choice to use your advice. Thanks again


Log in to reply