3.12 The delay Statement

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

A delay statement is used to halt execution of the enclosing task for a specified duration or until a specified time. It is a simple statement with two forms, the "relative delay" and the "absolute delay." Delays are often used in conjunction with select statements in multi-tasking programs, but they can also be used in single-thread programs, as illustrated below and in the next page. Both forms use the reserved word, delay. The absolute delay also uses the reserved word until.

Form of the relative delay

Form of the absolute delay

delay Duration_Expression;
delay until Time_Expression;

A duration_expression should evaluate to a predefined type called Duration. A time_expression should evaluate to the type, Time, provided by the predefined package Ada.Calendar. The following example illustrates the use of several relative delays. The example program in the next page also illustrates the use of relative delays. Examples of absolute delays are found in a later chapter on concurrency and synchronization.

The program is very simple, so we don't bother to depict its architecture graphically.

Source Code Listing

---------------------------------------------------------------------
--  This program illustrates the use of the relative delay statement.
---------------------------------------------------------------------
with Ada.Text_IO;
use  Ada.Text_IO;
procedure Do_Delays is
begin
  Put_Line("Wait 5 seconds.");
  delay 5.0;
  Put_Line("Wait 2 seconds.");
  delay 2.0;
  Put_Line("Wait 7 seconds.");
  delay 7.0;
  Put_Line("That's all folks."); 
end Do_Delays;
---------------------------------------------------------------------

If you compile, link and run the above, the program should produce the following output, and you will observe the appropriate time delays occurring between the display of successive lines of text.

     Wait 5 seconds.
     Wait 2 seconds.
     Wait 7 seconds.
     That's all folks.

Related Topics

6.2 The select Statement A.2 Simple and Compound Statements

[ Back to top of pagePrev ] Next ]