我刚刚使用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();
}