Search code examples
qt5qthreadqtconcurrentqsqldatabase

How to cancel QSqldatabase::open immediately in a running thread?


I have a Thread, which opens a database and emits a signal. The main idea is to check if the database is still there and valid. If the database is offline (MYSQL), QSqldatabase is trying for about 3 seconds to connect.

Now the problem: If I want to close the thread (Because UI Window got closed) and the database isn't reachable, I'll need to wait until the connection test is done, before I can close my thread and afterwards the UI.

So the UI can freeze, because it's waiting for the thread to close, which is busy with testing the connection.

Is there a possibility to abort a thread immediately? (I'm using Qt Concurrent::run).


Solution

  • After some years of experience I do know an answer: You shouldn't at all. Just leave it open or terminate, but the best option is to let it run, complete and then close the thread.

    I managed to keep the thread running until the test has finished. The UI is able to close at any time - and the database thread is following if busy.