How to stop a command that is taking a long time?



  • Hello everyone,

    I have a small app that executes a command that can potentially take a long time. In order to keep my GUI from freezing I created a worker that runs on a separate thread just so it can execute that command.

    What I want to do now is be able to abort the execution of that command using a pushbutton.

    This would have been easy if I had a for loop or something similar, but now that I just have one command I have no idea how to stop it (or how to stop the thread if thats better). Maybe as a reference you can imagine pressing Ctrl+C in cmd.


  • Moderators

    Is this "command" a sub process created with QProcess? Or is it just a "heavy" function in C++?

    You can stop a thread using "quit()":http://doc.qt.io/qt-5/qthread.html#quit, "terminate()":http://doc.qt.io/qt-5/qthread.html#terminate, or - if you are running a custom subclass - "requestInterruption()":http://doc.qt.io/qt-5/qthread.html#requestInterruption.



  • Hey sierdzio,

    This command is simply db.open() for a new database. In case there is something wrong with the credentials for example it may take up to 30 seconds just staying there trying to connect.

    thread.quit() did nothing for me and I am not sure I want to use terminate() as I am not a brave man.

    The last suggestion won't help because it is not a mater of informing the worker that I went to stop, but the act of stoping itself.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.