Tuesday 18 October 2011

RPGLE - Delay an RPG program

  RPGLE  - Delay an RPG program


PUTTING YOUR RPG PROGRAM TO SLEEP
Q: How do I insert pauses into my RPG program? In other words, how do I make
my RPG program go to sleep for a while?


You can use the Execute Command (QCMDEXC) API to run the DLYJOB command:

     D qcmdexc         pr                  ExtPgm('QCMDEXC')
     D   command                   3000A   const options(*varsize)
     D   length                      15P 5 const

      * wait for 5 seconds before continuing:
      *
     c                   callp     qcmdexc('DLYJOB DLY(5)': 13)

One nice thing about the DLYJOB command is that it can resume at a given
time:

     D qcmdexc         pr                  ExtPgm('QCMDEXC')
     D   command                   3000A   const options(*varsize)
     D   length                      15P 5 const

      * wait until 02:17 before continuing:
      *
     c                   callp     qcmdexc('DLYJOB RSMTIME(021700)': 22)

The sleep() API is nice because the source code is both shorter and easier
to read:

     H DFTACTGRP(*NO)

     D sleep           PR            10I 0 extproc('sleep')
     D   seconds                     10U 0 value

     c                   callp     sleep(10)

Sometimes you want to pause for less than a second, especially if you're
doing an animation such as scrolling text or graphics. The usleep() API
let's you specify your delay time in microseconds:

     H DFTACTGRP(*NO)

     D usleep          PR            10I 0 extproc('usleep')
     D   seconds                     10U 0 value

      * Note, usleep works with microseconds (one millionth of a second)
      *  so the following equates to one half of a second:

     c                   callp     usleep(500000)

The select() API (which is usually used in sockets programming) can also be
used to put your program to sleep. One advantage of select() is that you can
specify both whole seconds and fractional seconds in the same call:

     H DFTACTGRP(*NO)

     D select          PR            10I 0 extproc('select')
     D   max_fds                     10I 0 value
     D   read_set                      *   value
     D   write_set                     *   value
     D   excp_set                      *   value
     D   timeout                       *   value

     D timeval         ds
     D   tv_sec                      10I 0
     D   tv_usec                     10I 0

      *
      * tv_sec  = number of seconds
      * tv_usec = number of microseconds (one millionth of a second)
      *
      *   the following waits for 4.5 seconds:
      *

     c                   eval      tv_sec = 4
     c                   eval      tv_usec = 500000

     c                   callp     select(0: *NULL: *NULL: *NULL:
     c                                    %addr(timeval))



No comments:

Post a Comment