0

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).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ratloser
  • 116
  • 1
  • 7
  • 1
    You can force a thread to terminate immediately: `QThread::terminate()` – Ali Hosseini Nezhad Feb 11 '20 at 06:43
  • First, thank you for your comment :) Look like it's a method I'm searching for. Is there any similar method for Qt Concurrent? I'd like to prefer concurrent, since it's easier. (I won't make that much faults). – ratloser Feb 11 '20 at 11:21

1 Answers1

0

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.

ratloser
  • 116
  • 1
  • 7