C ++应用程序在退出时不会终止所有进程 [英] C++ application does not kill all processes on exit

查看:209
本文介绍了C ++应用程序在退出时不会终止所有进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的main.cpp,它启动mainwindow:

This is my main.cpp which starts the mainwindow:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TabWindow w;
    w.show();
    return a.exec();
}

即使使用 a.connect )我不明白为什么myApplication.exe仍然运行后,我关闭mainwindow。 有关点击退出按钮后,如何完全结束所有流程的任何建议?

Even with the a.connect(...) I do not understand why myApplication.exe still runs after I close the mainwindow. Any suggestions on how I can fully end all processes after the quit button is clicked?

编辑 a href =http://qt-project.org/doc/qt-5/qguiapplication.html#exec =nofollow> Qt文档说:
我们建议您连接清除代码到aboutToQuit()信号,而不是将它放在应用程序的main()函数中。这是因为在一些平台上,QApplication :: exec()调用可能不会返回。

The Qt Documentation says this: We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms, the QApplication::exec() call may not return.

推荐答案

@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!

这篇关于C ++应用程序在退出时不会终止所有进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆