(Contents)(Previous)(Next)

Preventing Multiple Instances of a DOS Program

Many DOS programs were written without any thought for multitasking and file sharing. So running multiple instances of the same program might create undesirable side effects. You can use a batch file to prevent users from running multiple instances accidentally. Create a batch file DOSPROG.BAT as illustrated in the following code sample, where DOSPROG is the original name of the program's EXE file.

Code Listing 7. Prevention of Multiple Instances of a DOS Program

@ECHO Off
DIR "c:\DOSPROG is already running" /b
IF EXIST "c:\DOSPROG is already running" GOTO End
REM > "c:\DOSPROG is already running"
START <path>\DOSPROG$ /W
DEL "c:\DOSPROG is already running"
:End

Rename the original program's EXE file to DOSPROG$.EXE. Add a command, identical to the next to last line in the above code listing, to your AUTOEXEC.BAT file to delete the semaphore file at start-up, so that you will start with a clean slate even if the DOS program crashes. Also edit all shortcuts that point to the executable file DOSPROG.EXE so they now point to the batch file DOSPROG.BAT.

When the batch file is executed, it first checks to see whether a zero-byte semaphore file is present in the root directory of C:\. If so, the program is already running and the batch file terminates. The DIR command, in its "bare" mode with the /b switch, generates the user notification with the zero-byte file's name as the message. If the semaphore file was not found, the batch file creates it and then launches the DOS program using the START command. The /W switch of the START command causes the START command to wait until the launched program terminates before returning control to the batch file. At this time the batch file executes the next line, which deletes the semaphore file. The system is now ready to run the DOS program again.


(Next)