comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R.Carter" <spam.jrcarter.not@spam.acm.org.not>
Subject: Re: wait does not perform as expected
Date: Fri, 24 Feb 2023 22:16:48 +0100	[thread overview]
Message-ID: <ttb9g0$2a2c1$1@dont-email.me> (raw)
In-Reply-To: <ac87b5c5-7c01-4af9-b9bf-8c3ffc3fe74dn@googlegroups.com>

On 2023-02-22 17:34, Daniel Gaudry wrote:
> the following code :

Atio and Tio are undefined. Clearly this code is not what you are running.

The important bit:

>           if Skip_After > 0.0
>              then
> 
>              -- KEEP THE USER'S ATTENTION
>              while Timer < Skip_After loop
>                 Timer := 1.0 + @;
>                 delay 1.0;
>                 ada.text_io.Put(Natural(Skip_After - Timer)'Img);
> 
>                 --USER ENDS THE WAITING PERIOD  BEFORE IT'S END ?
> 
> 
>                 TIO.LOOK_AHEAD(ITEM          => CHAR,
>                                END_OF_LINE   => HIT);
>                 
>                   ada.text_io.GET_IMMEDIATE(ITEM      => CHAR,
>                                      AVAILABLE => HIT);
>                    IF HIT THEN
>                       RETURN;
>                    END IF;
> 
>              end loop;
> 
>              -- USER WAITED FOR THE WHOLE  WAITING PERIOD
>              -- LET HIM READ THE ZERO ON THE SCREEN
> 
>              delay 1.0;
>              return;
>           end if;

Let's simplify this. Without the countdown, this can be rewritten using ATC

Timed_Out : Boolean := False;
...
select
    delay Skip_After;

    Timed_Out := True;
then abort
    Ada.Text_IO.Get_Immediate (Item => Char);
end select;

if Timed_Out then
    delay 1.0;
end if;

return;

This should be simple enough to get right. If ATC doesn't work well with this, 
it should still be simple enough to get right without it.

To add the countdown, let's split that out into a task:

declare
    task Timer is
       entry Stop;
    end Timer;

    task body Timer is ...
    -- Does the countdown immediately (uses the global constant Skip_After)
    -- Stops the countdown if Stop is called
begin
    select
       delay Skip_After;

       Timed_Out := True;
    then abort
       Ada.Text_IO.Get_Immediate (Item => Char);
       Timer.Stop;
    end select;
end;

if Timed_Out then
    delay 1.0;
end if;

return;

The task should be simple enough to get right, too.

-- 
Jeff Carter
"Saving keystrokes is the job of the text editor,
not the programming language."
Preben Randhol
64

      parent reply	other threads:[~2023-02-24 21:16 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-22 16:34 wait does not perform as expected Daniel Gaudry
2023-02-22 17:36 ` Niklas Holsti
2023-02-23 14:26   ` AdaMagica
2023-02-23 14:32     ` AdaMagica
2023-02-23 14:33     ` Daniel Gaudry
2023-02-23 14:39       ` AdaMagica
2023-02-23 17:15       ` Niklas Holsti
2023-02-23 17:35     ` Niklas Holsti
2023-02-23 17:49       ` Niklas Holsti
2023-02-23 18:14       ` Dmitry A. Kazakov
2023-02-23 18:29         ` Niklas Holsti
2023-02-23 18:47           ` Daniel Gaudry
2023-02-23 19:08             ` Niklas Holsti
2023-02-23 19:31             ` Niklas Holsti
2023-02-23 20:41           ` Dmitry A. Kazakov
2023-02-24 15:10             ` AdaMagica
2023-02-24 18:23               ` Niklas Holsti
2023-02-24 21:16 ` Jeffrey R.Carter [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox