Restrict multiple instances of an application in java
There is a library called jUnique which does that and will save you the bother of implementing it yourself.
There is a library called jUnique which does that and will save you the bother of implementing it yourself.
I believe that the problem is that singleInstance doesn’t let the callee activity to exist in the same task as the caller, hence it can’t return the value to the caller. Consider using singleTask instead: singleTask The system creates the activity at the root of a new task and routes the intent to it. However, … Read more
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
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
The Right Thing is advisory locking using flock(LOCK_EX); in Python, this is found in the fcntl module. Unlike pidfiles, these locks are always automatically released when your process dies for any reason, have no race conditions exist relating to file deletion (as the file doesn’t need to be deleted to release the lock), and there’s … Read more
A good way is: #include <sys/file.h> #include <errno.h> int pid_file = open(“/var/run/whatever.pid”, O_CREAT | O_RDWR, 0666); int rc = flock(pid_file, LOCK_EX | LOCK_NB); if(rc) { if(EWOULDBLOCK == errno) ; // another instance is running } else { // this is the first instance } Note that locking allows you to ignore stale pid files (i.e. … Read more
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
I use the following method in the main method. This is the simplest, most robust, and least intrusive method I have seen so I thought that I’d share it. private static boolean lockInstance(final String lockFile) { try { final File file = new File(lockFile); final RandomAccessFile randomAccessFile = new RandomAccessFile(file, “rw”); final FileLock fileLock = … Read more