Important: Please read the Qt Code of Conduct -

CommitData without killings QThreads

  • Hi guys,

    I'm trying to detect a system shutdown in my Windows application, as I would like the user to confirm if they would like to cancel the on-going processes before closing (and cancel the shutdown if not). As such, I am using QApplications' commitData() method. However the problem I am having is that as soon as the commitData() method is called, any QThreads I have running have already been destroyed - and I would only like to destroy them if the user accepts!

    Any ideas how to get around this?

    My commitData() code so far is below. As pThread->isRunning() always returns false, the first if statement never gets executed and so Windows logs off without any QMessageBox's appearing.


    @void MyApplication::commitData(QSessionManager& manager)
    if(manager.allowsInteraction() && pThread->isRunning())
    int result = QMessageBox::warning(0, "Confirmation Required", "Are you sure you want to close?", QMessageBox::Yes | QMessageBox::No);

            case QMessageBox::Yes:
                //clean up files and kill my QThreads manually
            case QMessageBox::No:


  • I've narrowed this down a bit further, hopefully it may help!

    It isn't the thread exiting, it is the QProcess within the QThread.

    I have my QThread set to run a QProcess which I have in a while loop:


    When it exits this while loop (i.e. the process has finished), it checks the exit code and sends a signal. However when I log off, this while loop is exited (i.e. QProcess::waitForFinished() returns true).

    Any idea how I can avoid this?


  • I don't believe there's a solution for this. Your workstation is in shutdown mode and quite surely it has notified the processes started by your threads of that fact already. That processes behave well and shut down themselves. That's why they return from waitForFinished().

    The only way to prevent this, would be to patch the processes to not quit on that notification, but if you don't rule their source code, this seems quite impossible to me.

  • Hi Volker,

    Thanks for your reply, I feared as such - and I don't have access to the processes source unfortunately. Never hurts to ask though!

    Thanks again

Log in to reply