June wiki..not late this time, just written in the very last minutes :p ..enjoy.. ;*
For some applications, it might be important to ensure that only one instance of that application can run at one time in a particular machine. The example is an application that during it’s running time modifies a stored data, whether it’s in the registry or database. Several instances of this application running at the same time might result in data corruption.
There are some known method to achieve such behavior, some are generic and some other are language specific. In this article, several methods will be discussed.
Creating a lock file with process id
During it’s initialization, an application were to create a file. If the file is not exist, that means there aren’t any other instances of this application which are already running. Just before the application closes, it should delete the file in order to allow the application to run again later. Inside the file, the application should write the it’s process id. This process id is important to avoid trouble if the application crashes and does not exit normally. In such case, the application might not delete the file before it closes. Next time the application is run, it will notice that the lock file still exists, thus assuming that there’s another instance of the application already running. This will prevent the application to run again forever, except if the file is deleted manually. With the existence of the process id, when the application tries to run and notice that the lock file exists, it can check for the process id and see whether that particular process id is still up and running. If it does, the application exit, otherwise, the application would modify the file to contain the new process id.
Creating a file, reading the content, writing, and deleting it should not be a problem. However, most of us might not be to familiar to read a process id of an application, thus it might require a little exploration.
Creating a lock file and open the file
This method is probably a little simpler compared to the previous one. This does not include detecting process id of the application. You will only need to create, delete, open a stream, and close a document. The idea is the application keeps the stream of the file open during its lifetime, to avoid other instance from deleting it. When another instance starts, it will search for the lock file and tries to delete it. Since the file is open, the instance will not be able to delete it, and it will know that another instance is already running, hence exit. When the application crashes, the open stream from the file will automatically be closed, thus the next instance will be able to run.
Creating a lock file and lock the file
In Java, you can specifically lock a file and release it when you’re done. This lock will automatically released if your application exits, even when it’s due to crash. Here’s the example of locking and releasing the lock of a file using Java:
try {
File file = new File("filename");
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
FileLock lock = channel.lock();
lock = channel.tryLock();
lock.release();
channel.close();
} catch (Exception e) {
//do nothing
}
Creating a port listener
In all of the previous methods, when an instance cannot run because another instance is already running, the instance can exit. Another modification to that is probable is the instance can show a message box notifying user that another instance of the application is already running. To even make it better, you might want to open and bring forward the window of the already running instance. If you want to do this, then probably this method is a good choice. The idea of this method is to make your application listens to a particular port. When you try to listen to a port that has already been listened to, then you will receive an exception. This means, another instance is already running. Then, to make the already running application open its windows, bring the windows to focus, or whatever you would like it to do, you send messages through the port. Just make sure that when you implement the listener, you implement them to react accordingly to the message they receive from the port. This method, despite of its attractiveness to allow you to send message to the already running instance, also have a drawback. Even though we can specify the application to listen to a rarely used port, there still be chances that another application is using the exactly same port, which will prevent out application to ever run.
Use port listener and lock file
Another method you can use is combining the port listener and a lock file. The application should first check whether the lock file exists, if it’s doesn’t exist, the application will try to search for an open port, create the lock file, write the port number in the lock file and listens to the port. On termination of the application, the application should then release the listener and deletes the file. Another instance of the application will check the existence of the lock file. Once it found the lock file, it tries to listen to the port number written in the file. If it succeeded, it means the previous instance might terminates abnormally, but is not running anymore, thus it can run. Otherwise, it means the previous instance is still running and thus the new instance should exit.
Other methods
Other than these methods, you might also be able to fine methods, including the language specific ones, here are some examples:
- For .NET

Recent Comments