readline
readline copied to clipboard
Add support for CancellationToken on Read/ReadPassword
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
Async versions of Console.ReadKey
and Console.ReadLine
with CancellationToken
are notably missing, but I think we should follow convention and:
- Create async versions of the methods (
ReadKeyAsync
,ReadLineAsync
etc.) - Return
Task<ConsoleKeyInfo>
andTask<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
)