Microsoft KB Archive/65505

How to Code a Delay in an OS/2 COBOL Program Using DosSleep

PSS ID Number: Q65505 Article last modified on 11-19-1990

3.00 3.00a 4.00 OS/2

Summary: COBOL versions 3.00, 3.00a, and 4.00 protected mode programs can be made to delay their execution for a specified period of time (down to a millisecond). This is accomplished by calling the OS/2 API function DosSleep. Below is a sample program that illustrates how to use DosSleep. This article applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS OS/2, and to Microsoft COBOL Professional Development System (PDS) version 4.00 for MS OS/2.

More Information: The OS/2 API function DosSleep takes one parameter, a double word (DWORD) that contains the amount of time to delay execution in milliseconds. Note that a DWORD in COBOL has a picture clause of 9(8) COMP-5. COBOL can directly call API functions by prefixing their names with a double underscore (__) and linking with the DOSCALLS.LIB library. The following sample program (COBDELAY.CBL) prompts the user for a specified delay in milliseconds and then beeps every time the delay has elapsed (it subtracts from the user-entered delay the time it takes to sound the beep – 125 milliseconds). It continues this until a key is pressed. To compile and link the program in COBOL 3.00/3.00a, enter the following commands at the OS/2 command prompt: pcobol cobdelay; link /nop cobdelay,,,pcobol doscalls; The compile and link lines for COBOL 4.00 PDS are as follows: cobol cobdelay; link cobdelay,,,coblib os2;

COBDELAY.CBL
$SET ANS85 IDENTIFICATION DIVISION. PROGRAM-ID. CobDelay. DATA DIVISION. WORKING-STORAGE SECTION. *  Delay in milliseconds passed to DosSleep. 01 Delay      PIC 9(8) COMP-5 VALUE 1000. *  Used to check if a key is in the keyboard buffer. 01 KeyStatus  PIC X           VALUE X&quot;00&quot;. PROCEDURE DIVISION. *  Get the delay desired from the user and subtract from it  *   the time it takes to sound the beep. DISPLAY &quot;Enter the delay desired (in milliseconds):&quot;. ACCEPT Delay. COMPUTE Delay = Delay - 125. *  Beep and delay until a key is pressed. PERFORM UNTIL KeyStatus NOT = X&quot;00&quot; CALL X&quot;E5&quot; CALL &quot;__DosSleep&quot; USING BY VALUE Delay CALL X&quot;D9&quot; USING KeyStatus END-PERFORM. STOP RUN. Copyright Microsoft Corporation 1990.