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

Marco van de Voort marcov at stack.nl
Fri Dec 9 17:03:31 CET 2011


In our previous episode, Michael Van Canneyt said:
> > 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;
> 
> You must use fpSelect() or Poll on the handle on linux/unix.
> On windows, you'll need to WaitForObject() or so on the file handle.

Be careful on streams that might have buffers though. Then you first need to
check if there are still bytes in the buffer.



More information about the fpc-pascal mailing list