且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++应用程序在退出时不会终止所有进程

更新时间:2022-01-21 23:11:39

@ratchetfreak 在我的问题,我想出了问题在哪里。

Thanks to the comment posted by @ratchetfreak in my question, I figured out where the problem was.

在我的MainWindow中,我开始未终止的工作线程,因此在应用程序关闭后仍然保存为进程。为了解决这个问题,我注册了关闭事件并且跟踪线程的存在 - 即基本上忽略了closeEvent,直到线程也被删除。

In my MainWindow, I started a worker thread which was not terminated and thus still persisted as a process after the application was closed. In order to fix this, I registered the close event and kept track of the existence of the thread - i.e. basically, ignored the closeEvent until the thread was deleted as well.

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (workerThreadExists) {
        // Gracefully exiting all tasks inside the worker thread
        while (workerThreadExists){
            event->ignore();
        }
        event->accept();

    }
}

code> workerThreadExists 只是一个 BOOLEAN ,一旦创建线程设置为true,然后当线程设置为false被删除。希望这有助于!

...and for me workerThreadExists is just a BOOLEAN that is set to true once the thread is created and then it is set to false when the thread is deleted. Hope this helps!