[fpc-pascal] How to poll for a byte in Input?

tcoq tcoq at free.fr
Fri Dec 9 16:44:38 CET 2011


Hello,
I'm trying to poll the input stream to see whether there is a new character available, without blocking my software.
I've tried "Read" and "THandleStream.ReadByte" but with no success: when the input is empty, the program just waits.

Is there a way to ask whether the input stream is not empty, without waiting and without using threads/processes?
And, in addition, is there an OS-independent way (linux, windows)?

something like:
var 
  inputStream: THandleStream;
  aByte: Byte;
begin
  InputStream := THandleStream.Create(StdInputHandle);
  try
    while true do
    begin
      {$I-}
      aByte := InputStream.ReadByte;
      DoSomethingWithByte(aByte);
      result := true;
    end;
  except
    On E: EStreamError do result := false;
  end;
  //This code here is never executed
  ThereIsNothingInInputStream_DoDefault;
end;
  

(PS. In windows, I traced the "readByte" procedure down to a Windows API function: ReadFile in Kernel32. This seems to be a blocking function).
Best regards,
Thierry



More information about the fpc-pascal mailing list