Skip to content

Commit

Permalink
remove localsocket, check if process is already running with tasklist…
Browse files Browse the repository at this point in the history
… instead
  • Loading branch information
Odizinne committed Nov 10, 2024
1 parent 0a43476 commit b9effdc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion BigPictureTV.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
QT += core gui network
QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

Expand Down
1 change: 1 addition & 0 deletions Utils/Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Utils.h"
#include <QTranslator>
#include <QApplication>
#include <QScreen>
#include <QDebug>
Expand Down
45 changes: 19 additions & 26 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
#include <QApplication>
#include <QDebug>
#include <QLocale>
#include <QMessageBox>
#include <QLocalServer>
#include <QLocalSocket>
#include <QString>
#include <QTranslator>
#include <QProcess>
#include "BigPictureTV.h"
#include "Utils.h"

int main(int argc, char *argv[])
bool isAnotherInstanceRunning(const QString& processName)
{
QApplication a(argc, argv);

QString serverName = "BigPictureTVUniqueIdentifier";
QLocalServer localServer;
QLocalSocket localSocket;
QProcess process;
process.start("tasklist", QStringList() << "/FI" << QString("IMAGENAME eq %1").arg(processName));
process.waitForFinished();
QString output = process.readAllStandardOutput();
int count = output.count(processName, Qt::CaseInsensitive);

localSocket.connectToServer(serverName);
return count > 1;
}

if (localSocket.waitForConnected(500)) {
qDebug() << "Another instance is already running.";
return 0;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
if (Utils::isWindows10()) {
a.setStyle("fusion");
}

if (!localServer.listen(serverName)) {
qDebug() << "Unable to start the local server:" << localServer.errorString();
return 1;
const QString processName = "BigPictureTV.exe";
if (isAnotherInstanceRunning(processName)) {
qDebug() << "Another instance is already running. Exiting...";
return 0;
}

QLocale locale;
QString languageCode = locale.name().section('_', 0, 0);
QTranslator translator;

if (translator.load(":/translations/BigPictureTV_" + languageCode + ".qm")) {
a.installTranslator(&translator);
}

if (Utils::isWindows10()) {
a.setStyle("fusion");
}
a.setQuitOnLastWindowClosed(false);

BigPictureTV w;

QObject::connect(&a, &QApplication::aboutToQuit, [&localServer]() {
localServer.close();
});

return a.exec();
}

0 comments on commit b9effdc

Please sign in to comment.