Add shared memory back into main.cpp

This commit is contained in:
Ryan Huffman 2016-01-04 11:58:56 -08:00
parent 9520bd7ba7
commit 471c555bca

View file

@ -44,7 +44,18 @@ static BOOL CALLBACK enumWindowsCallback(HWND hWnd, LPARAM lParam) {
int main(int argc, const char* argv[]) {
QString applicationName = "High Fidelity Interface - " + qgetenv("USERNAME");
// Connect to and send message to existing interface instance
bool instanceMightBeRunning = true;
#ifdef Q_OS_WIN
// Try to create a shared memory block - if it can't be created, there is an instance of
// interface already running. We only do this on Windows for now because of the potential
// for crashed instances to leave behind shared memory instances on unix.
QSharedMemory sharedMemory { applicationName };
instanceMightBeRunning = !sharedMemory.create(1, QSharedMemory::ReadOnly);
#endif
if (instanceMightBeRunning) {
// Try to connect and send message to existing interface instance
QLocalSocket socket;
socket.connectToServer(applicationName);
@ -79,6 +90,18 @@ int main(int argc, const char* argv[]) {
return EXIT_SUCCESS;
}
#ifdef Q_OS_WIN
return EXIT_SUCCESS;
#endif
}
// Setup local server
QLocalServer server;
// We failed to connect to a local server, so we remove any existing servers.
server.removeServer(applicationName);
server.listen(applicationName);
QElapsedTimer startupTime;
startupTime.start();
@ -102,13 +125,6 @@ int main(int argc, const char* argv[]) {
QSettings::setDefaultFormat(QSettings::IniFormat);
Application app(argc, const_cast<char**>(argv), startupTime);
// Setup local server
QLocalServer server { &app };
// We failed to connect to a local server, so we remove any existing servers.
server.removeServer(applicationName);
server.listen(applicationName);
QObject::connect(&server, &QLocalServer::newConnection, &app, &Application::handleLocalServerConnection);
QTranslator translator;