Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CancellationToken on Read/ReadPassword #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

oblaise
Copy link

@oblaise oblaise commented Dec 11, 2020

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

@BloodBaz
Copy link

BloodBaz commented Jun 7, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants