PyQt – how to detect and close UI if it’s already running?

Here is a very simple PyQt5 solution using QLockFile: from PyQt5 import QtCore, QtWidgets lockfile = QtCore.QLockFile(QtCore.QDir.tempPath() + ‘/my_app_name.lock’) if lockfile.tryLock(100): app = QtWidgets.QApplication([]) win = QtWidgets.QWidget() win.setGeometry(50, 50, 100, 100) win.show() app.exec() else: print(‘app is already running’) There were a couple of fairly straightforward C++ solutions given on the Qt Wiki which no longer … Read more

Run the current application as Single Instance and show the previous instance

I propose you a different method, using a combination of the System.Threading.Mutex class and UIAutomation AutomationElement class. A Mutex can be, as you already know, a simple string. You can assign an application a Mutex in the form of a GUID, but it can be anything else. Let’s assume this is the current Application Mutex: … Read more

Is using a Mutex to prevent multiple instances of the same program from running safe?

It is more usual and convenient to use Windows events for this purpose. E.g. static EventWaitHandle s_event ; bool created ; s_event = new EventWaitHandle (false, EventResetMode.ManualReset, “my program#startup”, out created) ; if (created) Launch () ; else Exit () ; When your process exits or terminates, Windows will close the event for you, and … Read more