Delay.st
changeset 1316 248a8cb2ae3b
parent 1294 e26bbb61f6b2
child 1348 183876a4e085
equal deleted inserted replaced
1315:85a27a31a690 1316:248a8cb2ae3b
    54     (1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will
    54     (1/100, 1/60, 1/50, or even 1/20 of a second). Thus very small delays will
    55     delay for at least this minimum time interval. See examples.
    55     delay for at least this minimum time interval. See examples.
    56 
    56 
    57     [see also:]
    57     [see also:]
    58         Semaphore Process ProcessorScheduler
    58         Semaphore Process ProcessorScheduler
       
    59         Time AbsoluteTime
    59         (using delay : programming/timing.html#DELAY)
    60         (using delay : programming/timing.html#DELAY)
    60 
    61 
    61     [author:]
    62     [author:]
    62         Claus Gittinger
    63         Claus Gittinger
    63 "
    64 "
    65 
    66 
    66 examples 
    67 examples 
    67 "
    68 "
    68     Check your systems resolution with:
    69     Check your systems resolution with:
    69     (make certain, that no other timed processes are running in the background when doing this)
    70     (make certain, that no other timed processes are running in the background when doing this)
    70 
    71                                                                         [exBegin]
    71         |d t1 t2 res|
    72         |d t1 t2 res|
    72 
    73 
    73         Processor activeProcess priority:24.
    74         Processor activeProcess priority:24.
    74         t1 := Time millisecondClockValue.
    75         t1 := Time millisecondClockValue.
    75         d := Delay forMilliseconds:1.
    76         d := Delay forMilliseconds:1.
    76         100 timesRepeat:[d wait].
    77         100 timesRepeat:[d wait].
    77         t2 := Time millisecondClockValue.
    78         t2 := Time millisecondClockValue.
    78         res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100.
    79         res := (OperatingSystem millisecondTimeDeltaBetween:t2 and:t1) // 100.
    79         Transcript show:'minimum delta is about '; show:res; showCr:' milliseconds'.
    80         Transcript show:'minimum delta is about '; show:res; showCr:' milliseconds'.
    80         Processor activeProcess priority:8.
    81         Processor activeProcess priority:8.
       
    82                                                                         [exEnd]
    81 
    83 
    82 
    84 
    83     examples:
    85     examples:
    84 
    86 
    85         delaying for some time-delta:
    87     delaying for some time-delta:
    86         (notice: you cannot use this without time-errors in a loop,
    88     (notice: you cannot use this without time-errors in a loop,
    87          since the errors will accumulate; after 5 runs through the loop,
    89      since the errors will accumulate; after 5 runs through the loop,
    88          more than 5 seconds have passed)
    90      more than 5 seconds have passed)
    89 
    91 
    90                 |d|
    92             |d|
    91                 d := Delay forMilliseconds:500.
    93             d := Delay forMilliseconds:500.
    92                 10 timesRepeat:[d wait]
    94             10 timesRepeat:[d wait]
    93 
    95 
    94         prove:
    96     prove:
    95                 |d t1 t2 deltaT|
    97                                                                     [exBegin]
    96                 d := Delay forMilliseconds:500.
    98             |d t1 t2 deltaT|
    97                 t1 := Time millisecondClockValue.
    99             d := Delay forMilliseconds:500.
    98                 10 timesRepeat:[
   100             t1 := Time millisecondClockValue.
    99                     d wait
   101             10 timesRepeat:[
   100                 ].
   102                 d wait
   101                 t2 := Time millisecondClockValue.
   103             ].
   102                 deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
   104             t2 := Time millisecondClockValue.
   103                 Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
   105             deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
   104 
   106             Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
   105         delaying until a specific time is reached:
   107                                                                     [exEnd]
   106         (this can be used to fix the above problem)
   108 
   107 
   109     delaying until a specific time is reached:
   108                 |now then t1 t2 deltaT|
   110     (this can be used to fix the above problem)
   109 
   111                                                                     [exBegin]
   110                 t1 := Time millisecondClockValue.
   112             |now then t1 t2 deltaT|
   111                 now := Time millisecondClockValue.
   113 
   112                 10 timesRepeat:[
   114             t1 := Time millisecondClockValue.
   113                     then := OperatingSystem millisecondTimeAdd:now and:1000.
   115             now := Time millisecondClockValue.
   114                     (Delay untilMilliseconds:then) wait.
   116             10 timesRepeat:[
   115                     now := then
   117                 then := OperatingSystem millisecondTimeAdd:now and:1000.
   116                 ].
   118                 (Delay untilMilliseconds:then) wait.
   117                 t2 := Time millisecondClockValue.
   119                 now := then
   118                 deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
   120             ].
   119                 Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
   121             t2 := Time millisecondClockValue.
   120 
   122             deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
   121         instead of recreating new delays, you can also reuse it:
   123             Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
   122 
   124                                                                     [exEnd]
   123                 |d now then t1 t2 deltaT|
   125 
   124 
   126     instead of recreating new delays all over, 
   125                 t1 := Time millisecondClockValue.
   127     you can also reuse it:
   126                 now := Time millisecondClockValue.
   128                                                                     [exBegin]
   127                 d := Delay new.
   129             |d now then t1 t2 deltaT|
   128                 10 timesRepeat:[
   130 
   129                     then := OperatingSystem millisecondTimeAdd:now and:1000.
   131             t1 := Time millisecondClockValue.
   130                     d resumtionTime:then.
   132             now := Time millisecondClockValue.
   131                     d wait.
   133             d := Delay new.
   132                     now := then
   134             10 timesRepeat:[
   133                 ].
   135                 then := OperatingSystem millisecondTimeAdd:now and:1000.
   134                 t2 := Time millisecondClockValue.
   136                 d resumtionTime:then.
   135                 deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
   137                 d wait.
   136                 Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
   138                 now := then
       
   139             ].
       
   140             t2 := Time millisecondClockValue.
       
   141             deltaT := OperatingSystem millisecondTimeDeltaBetween:t2 and:t1.
       
   142             Transcript show:'average delay: '; show:deltaT // 10; showCr:' milliseconds'
       
   143                                                                     [exEnd]
   137 "
   144 "
   138 ! !
   145 ! !
   139 
   146 
   140 !Delay class methodsFor:'instance creation'!
   147 !Delay class methodsFor:'instance creation'!
   141 
   148 
   259 ! !
   266 ! !
   260 
   267 
   261 !Delay class methodsFor:'documentation'!
   268 !Delay class methodsFor:'documentation'!
   262 
   269 
   263 version
   270 version
   264     ^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.20 1996-04-25 16:54:15 cg Exp $'
   271     ^ '$Header: /cvs/stx/stx/libbasic/Delay.st,v 1.21 1996-04-27 18:02:28 cg Exp $'
   265 ! !
   272 ! !