readline icon indicating copy to clipboard operation
readline copied to clipboard

Add support for CancellationToken on Read/ReadPassword

Open oblaise opened this issue 4 years ago • 1 comments

This PR adds CancellationToken to the Read and ReadPassword API.

Functionalities:

  • CancellationToken for Read and ReadPassword
  • Async API: ReadAsync
  • ReadKey to read a single key that mimics Console.ReadKey(bool)

This change is constructed using a background thread that perform readkey on the console and exchange them with the API calling thread using synchronization events. This approach avoid recurrent pulling on the Console.KeyAvailable.

Credits: This change was heavily inspired by 'JSquareD' answer on the following post: https://stackoverflow.com/questions/57615/how-to-add-a-timeout-to-console-readline

oblaise avatar Dec 11 '20 08:12 oblaise

Async versions of Console.ReadKey and Console.ReadLine with CancellationToken are notably missing, but I think we should follow convention and:

  1. Create async versions of the methods (ReadKeyAsync, ReadLineAsync etc.)
  2. Return Task<ConsoleKeyInfo> and Task<string> (respectively)

Also, there's inconsistent naming of the CancellationToken formal parameter (sometimes cancellationToken and sometimes just cancellation). This should consistently be cancellationToken or token)

BloodBaz avatar Jun 07 '21 15:06 BloodBaz