AsyncConsoleReader 1.0.2
dotnet add package AsyncConsoleReader --version 1.0.2
NuGet\Install-Package AsyncConsoleReader -Version 1.0.2
<PackageReference Include="AsyncConsoleReader" Version="1.0.2" />
<PackageVersion Include="AsyncConsoleReader" Version="1.0.2" />
<PackageReference Include="AsyncConsoleReader" />
paket add AsyncConsoleReader --version 1.0.2
#r "nuget: AsyncConsoleReader, 1.0.2"
#:package AsyncConsoleReader@1.0.2
#addin nuget:?package=AsyncConsoleReader&version=1.0.2
#tool nuget:?package=AsyncConsoleReader&version=1.0.2
AsyncConsoleReader
Provides a cancelable, non-blocking alternative to Console.Read()/ReadKey()/ReadLine().
English | 日本語
Overview
AsyncConsoleReader is a library that provides non-blocking standard input reading functionality with CancellationToken support.
In C#, Console.ReadLine() is commonly used for reading standard input, but it blocks the process until the reading is complete. Additionally, it cannot be canceled using mechanisms like CancellationToken. Even if wrapped in Task.Run, it occupies a thread from the thread pool until the input is finished.
AsyncConsoleReader provides a cancelable implementation of Read/ReadLine by re-implementing them using Console.ReadKey().
It also provides async APIs for efficiently performing reads on the thread pool.
The async APIs of AsyncConsoleReader are helpers for efficiently executing Read() on background threads. The reading itself is not performed asynchronously.
Installation
NuGet packages
To use AsyncConsoleReader, .NET 8.0 or higher is required. Packages can be obtained from NuGet.
.NET CLI
dotnet add package AsyncConsoleReader
Package Manager
Install-Package AsyncConsoleReader
Usage
You can call Read/ReadKey/ReadLine using AsyncConsole. These APIs are equivalent to System.Console but allow cancellation by passing a CancellationToken.
using AsyncConsoleReader;
var cts = new CancellationTokenSource();
cts.CancelAfter(500);
try
{
var line = AsyncConsole.ReadLine(cts.Token);
Console.WriteLine(line);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
You can also use the asynchronous API.
var line = await AsyncConsole.ReadKeyAsync(false, cts.Token);
License
This library is provided under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.