From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-3.2 required=3.0 tests=BAYES_00,NICE_REPLY_A, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: wait does not perform as expected Date: Wed, 22 Feb 2023 19:36:03 +0200 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net Fslr1YHdmY+E91kMY9cW3wX/feawH6UzCicQCoDspTFRVX3dDJ Cancel-Lock: sha1:FIghmg6YqxgiClZFQMZ9kfBMzhg= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Content-Language: en-US In-Reply-To: Xref: reader01.eternal-september.org comp.lang.ada:64959 List-Id: On 2023-02-22 18:34, Daniel Gaudry wrote: > hi > the following code : (I include only the relevant parts:) > 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; Why do you call both Look_Ahead and Get_Immediate, but throw away the result of Look_Ahead? The behaviour may depend on how you configure (via OS calls) the behaviour of your terminal emulator (the "shell window"), but for me, on Mac OS/X with the default configuration, it behaves as follows. The first time Look_Ahead is called (first loop iteration) it waits for the user to press Return (Enter) before it delivers the look-ahead character to the program. It must do so because it must see if the next input is an end-of-line (Return/Enter) or a character (assuming there is no "unconsumed" input when the loop starts). The program is therefore suspended in the first Look_Ahead call, and the timer loop and remaining-time output are also suspended, until you at least press Return/Enter. If you do press Return/Enter, perhaps after pressing some other keys, the waiting Look_Ahead call returns either the (first) character you pressed before Return/Enter, or End_Of_Line (Hit is True). However, your code then throws those results away, and instead calls Get_Immediate. On my system, Get_Immediate does /not/ use the inputs collected (but not "consumed") by Look_Ahead, but instead checks if there are _new_ input keystrokes. If there are none, Get_Immediate returns "not Available" (Hit is False) and so the loop continues. When Look_Ahead is called again, in the further loop iterations, it sees that there now is unconsumed input (from the first call of Look_Ahead) so it returns immediately (with the same results as on the first call). Therefore the loop now works are you intend: if the user presses any key before the Skip_After time is exhausted, the subprogram returns when Get_Immediate sees that keystroke. To fix the problem, remove the Look_Ahead call from the loop. (For goodness sake, also clean up your "with" clauses -- only Ada.Text_IO is relevant for your code -- and remove the multiple renames of Ada.Text_IO. You seem to have at least "Atio" and "TIO" as renames for Ada.Text_IO. One would be plenty.)