You are currently browsing the monthly archive for June, 2008.

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 Delphi

- For .NET

I’ve made up my mind,
Don’t need to think it over
If I’m wrong, I am right
Don’t need to look no further,
This ain’t lust
I know this is love
But, if I tell the world
I’ll never say enough
Cos it was not said to you
And that’s exactly what I need to do
If I end up with you

Should I give up,
Or should I just keep chasin’ pavements?
Even if it leads nowhere
Or would it be a waste
Even if I knew my place
Should I leave it there
Should I give up,
Or should I just keep chasin’ pavements
Even if it leads nowhere

I build myself up
And fly around in circles
Waitin’ as my heart drops
And my back begins to tingle
Finally, could this be it

Or should I give up
Or should I just keep chasin’ pavements
Even if it leads nowhere
Or would it be a waste
Even if I knew my place
Should I leave it there

Should I give up
Or should I just keep chasin’ pavements
Even if it leads nowhere
Or would it be a waste
Even if I knew my place should I leave it there
Should I give up
Or should I just keep on chasin’ pavements
Should I just keep no chasin’ pavements

[by adele]

Ahaha, tiba-tiba postingannya gini.. Jadi, di kantor sebulan sekali gw harus menulis di wiki lokal sana. Karena menulis buat gw merupakan suatu hal yang tidak terjadi secara alamiah sehingga menghabiskan waktu dan tenaga, sayang dong kalo adanya di wiki lokal aja. Jadi di tulis disini juga. Selain itu, sekarang gw uda telat blm nulis, bsk senin ga ke kantor, jadi ditulis disini supaya asisten saya saudari miri bisa memasukannya ke wiki, bukankah begitu mir?hihihi.. becanda mir..makasi yaaaa.. salam buat live update..kasitau kalo ito marah2 karena gabisa baca kode gw yg brantakan..hii, deg2an.. Baiklah, enuff chit chat, mari kita mulai tulisan cupu sok2 technicalnya:


To develop a desktop application that requires an internet connection, we might need to create a connection using proxy. Though in some cases we can ask user to provide the proxy setting that we need to use, in many other cases, the user themselves might not know the proxy setting that they are using. In such cases, detecting the proxy setting automatically might just be the answer. In this article, we will discuss on how to detect the proxy setting in Windows.

Proxy settings in windows
To know what the available proxy settings in Windows are, we can see the settings in the Internet Explorer. There are several options available:

1. Automatically detect settings.

2. Use automatic configuration script.

3. Use a proxy server for your LAN (manual configuration).

Where the information are stored

Information about this setting can be retrieved in the registry.

The registry key you should be looking for are available in the following location:
HKEY_CURRENT_USER>Software>Microsoft>Windows>CurrentVersion>Internet Settings

How to determine the setting

Different proxy settings will create different keys or different values in the registry as follows:

  • ProxyEnable: this key can be set to 0 or 1. The value 1 specifies that the manual configuration is used.
  • ProxyServer: if we specify manual configuration, the Address and Port of the proxy server is specified in this key with the format of <server address>:<server port> (i.e. “127.0.0.0:8080″)
  • ProxyOverride: for manual proxy settings, sometimes some local addresses will require you to bypass the proxy in order to be able to establish the connection. This key specifies which addresses are suppose to be bypassed. Different addresses are separated with a semicolon.
  • AutoConfigURL: when we specify to use an automatic configuration file for the proxy, the address to the automatic configuration file is available as a value in this key. The configuration file is a Proxy Auto Configuration (.pac) file. This file contains javascript functiona which will determine which proxy to use to connect to a particular server address.

Implementation tips

  • Reading Registry. To implement automatic detection of proxy setting in Windows, first, we need to be able to read the registry. For Java, we can use library such as the jRegistryKey to read and write to the registry.
  • Using .pac file. As a .pac file contains javascripts, it is most likely that we will not be able to use the file directly except if we create our on interpreter for javascript. However, a library is already available to parse such file, such as the pacparser. This library is a C library which allows you to retrieve the proxy that you should use to connect to a particular server address according to the .pac file.