简体   繁体   English

如何启动在应用程序退出时不会关闭的线程? (Qt C ++)

[英]How to start a thread that doesn't close on application quit? (Qt C++)

I'm making an application for Mac OS X in Qt, and wanted to spawn a thread that doesn't close on application close. 我正在Qt中为Mac OS X开发一个应用程序,并希望产生一个在应用程序关闭时不会关闭的线程。

Is this possible? 这可能吗? If so, how? 如果是这样,怎么办? I don't want the process to be stopped if a user force quits the application. 如果用户强制退出应用程序,我不希望该过程停止。

Thanks in advance. 提前致谢。

Note: If this is not possible, is there any way I can make this happen? 注意:如果这不可能,我有什么办法可以做到这一点? (Maybe with calling a command in bash?) (也许是用bash调用命令?)

it's possible to achieve your goal by initiating a new process via QProcess::startDetached, as per documents http://doc.qt.io/qt-4.8/qprocess.html#startDetached 根据文档http://doc.qt.io/qt-4.8/qprocess.html#startDetached ,通过QProcess :: startDetached启动新流程可以实现您的目标

Starts the program program with the arguments arguments in a new process, and detaches from it. 在新进程中启动带有arguments arguments的程序,然后从中分离。 Returns true on success; 成功返回true; otherwise returns false. 否则返回false。 If the calling process exits, the detached process will continue to live . 如果调用进程退出,则分离的进程将继续运行

Unix: The started process will run in its own session and act like a daemon. Unix:启动的进程将在其自己的会话中运行,并充当守护程序。

Edit: 编辑:

Here is an example for MacOS 这是MacOS的示例

// run start script
QString scriptPath = "path-to-start-script/start.sh" ;
QString cmd = "open -a " + scriptPath;
QProcess::startDetached(cmd);

When you terminate a process, all threads within that process die - the process is the thread "container". 当您终止一个进程时,该进程中的所有线程都会死亡-该进程是线程“容器”。 If you want to spawn something that lives on beyond your current process, then spawn a new independant process. 如果您想产生超出当前过程的生命,请产生一个新的独立过程。

If your user force-quits the application, that usually means that they want to control the energy or resource expenditure on their system. 如果您的用户强行退出该应用程序,则通常意味着他们要控制系统上的能源或资源支出。 Thinking that you know better than the user would be rather in the user's face. 认为您比用户更了解会面对用户。 Don't anger your users :) 不要激怒您的用户:)

All you really want is: 您真正想要的是:

  1. For the process to survive quitting it through normal means (pressing ⌘-Q, selecting Quit from the menu, closing the last window, etc.). 为了使该进程能够正常退出(按means-Q,从菜单中选择“退出”,关闭最后一个窗口等),以便生存。
  2. For the dock icon to disappear after the GUI has been quit, as is the normal and expected behavior. 退出GUI后,使停靠图标消失,这是正常现象和预期行为。
  3. For a cleanup code to run after the GUI has vanished. 要在GUI消失后运行清除代码。

To that effect, you only need to hide the dock icon/menubar and then perform the cleanup: 为此,您只需要隐藏停靠图标/菜单 ,然后执行清理:

main.mm main.mm

// https://github.com/KubaO/stackoverflown/tree/master/questions/hidedock-39378276
#include <QtWidgets>
#include <AppKit/AppKit.h>

void hideDockIcon() {
    [NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited];
}

int main(int argc, char ** argv) {
    QApplication app{argc, argv};
    QLabel label{"Quit Me"};
    label.setMinimumSize(200, 100);
    label.show();
    int rc = app.exec();
    hideDockIcon();
    qDebug() << "cleaning up";
    QThread::sleep(5);
    qDebug() << "cleanup finished";
    return rc;
}

hidedock-39378276.pro hidedock-39378276.pro

QT = widgets
CONFIG += c++11
TARGET = hidedock-39378276
TEMPLATE = app
OBJECTIVE_SOURCES += main.mm
LIBS += -framework AppKit

If u mean closing of Gui window by "Application Close" then it can be done easily... 如果您要通过“应用程序关闭”关闭Gui窗口,则可以轻松完成...

#include "MainWindow.h"
#include <QApplication>

//////////////////////////////////////
#include <thread>
struct blabla {
    std::thread th;
    blabla():th([]{
        // body of your thread  
    }) {}
    ~blabla(){
        if(th.joinable())
            th.join();
    }   
} singleton_obj;
/////////////////////////////////////

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Now the applicaton window will quit but the daemon thread will run n background... Enjoy 现在,applicaton窗口将退出,但守护进程线程将在后台运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM