使用Windows中另一个进程的事件循环向Qt应用程序发送退出消息
Send a Quit message to Qt app with event loop from another process in Windows
我用Qt和QTcpServer
创建了一个服务器。它在后台运行,不显示任何窗口,但它使用事件循环。
我的主.cpp看起来像这样:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyServer theServer;
return a.exec();
}
如何在不诉诸TerminateProcess()
的情况下通知我的服务器关闭?我对仅限Windows的解决方案很好,因此如果需要,我可以使用WINAPI功能。
根据服务器的用途,当您使用 TCPServer 时,您可以向它发送一条消息以告知它退出,尽管您可能希望验证谁在发送该消息。
或者,在同一台计算机上有一个控制器应用程序,该应用程序可以通过命名管道与服务器通信,您可以使用命名管道告诉它退出。
我只是使用 QLocalServer
实现它。事实证明,这很容易:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
static const char *quitSignalName = "My Service Quit Signal";
const QStringList &args = a.arguments();
if (args.size() == 2 && args[1] == "--shutdown") {
// Connect to the named pipe to notify the service it needs
// to quit. The QLocalServer will then end the event loop.
QLocalSocket quitSignal;
quitSignal.connectToServer(quitSignalName);
quitSignal.waitForConnected();
return 0;
}
// Listen for a quit signal, we connect the newConnection() signal
// directly to QApplication::quit().
QLocalServer quitSignalWatcher;
QObject::connect(&quitSignalWatcher, SIGNAL(newConnection()), &a, SLOT(quit()));
quitSignalWatcher.listen(quitSignalName);
MyServer theServer;
Q_UNUSED(theServer);
return a.exec();
}
相关文章:
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 如何在qt中同步应用程序和显示器的刷新率?
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- Qt QML桌面应用程序自动化测试
- Qt creator 4.11,在应用程序输出面板中创建一个链接
- 使用 Bazel 编译 QT 应用程序时访问资源
- 将 Crashpad 与 Windows Qt 应用程序集成
- 在Visual Studio中单实例Qt应用程序版本5.11.1中,在所有其他窗口的顶部打开Qt MainWindow
- 在 Ubuntu 服务器上运行 QT 应用程序时崩溃
- 如何在Android设备上解析Qt应用程序的ClassNotFoundEception?
- Qt应用程序找不到第三方DLL并崩溃
- 在 Linux 上部署 Qt 应用程序
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- QT 样式表主题,适用于使用属性选择器的整个应用程序
- 构建一个真正、完全静态的Qt 5框架和应用程序(Qt 5.8之后)
- 无法从对话框中关闭应用程序(Qt 应用程序)
- 将Poco C++库包含到Mac OS X上的Qt Quick应用程序(Qt Creator)中
- 独立应用程序Qt
- 打开文件时基于 Qt 的应用程序'Qt platform plugin windows not found'问题
- 我的应用程序"Qt"在我的计算机中运行,但在我朋友的计算机上无法运行