Microsoft KB Archive/69173

INF: Ensure Only One Copy of Your Program Is Running at a Time PSS ID Number: Q69173 Article last modified on 08-13-1991 PSS database name: O_Os2SDK

1.21 1.30

OS/2

Summary:

Listed below is a method an application can use to check to see if another instance of it is already running, and just return if there is one already running.

Since there isn’t a documented API call that performs this type of functionality, the easiest way to do this is to create a semaphore that has a unique name. If the semaphore is created without any errors, this is the first instance of your application. If the return code is ERROR_ALREADY_EXISTS, the application is already running and the new instance of it should exit. This is not the normal usage for semaphores; however, it is still effective. For example:

usRetCode=DosCreateSem(usFlag,&hsem,“\sem\unique”); if (usRetCode==ERROR_ALREADY_EXISTS) DosExit(EXIT_PROCESS,0);

Note: You should make sure that the semaphore name you use is unique; otherwise, if two different programs are using this technique, it might mean that one of the programs wouldn’t be able to run.

Copyright Microsoft Corporation 1991.