[PR #414] [MERGED] ReadConsoleInputStream demo #24108

Open
opened 2026-01-31 09:01:26 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/microsoft/terminal/pull/414
Author: @oising
Created: 4/21/2019
Status: Merged
Merged: 4/25/2019
Merged by: @miniksa

Base: masterHead: readconsoleinputstream-demo


📝 Commits (5)

  • cade139 initial commit for tools/readconsoleinputstream PR
  • 7eea98d add sln file
  • 2dc178b address issues and suggestions in PR review
  • 5bd3f88 remove empty xmldoc
  • cfe3eb9 address PR comments (root namespace)

📊 Changes

6 files changed (+474 additions, -0 deletions)

View changed files

tools/ReadConsoleInputStream/ConcurrentBoundedQueue.cs (+58 -0)
tools/ReadConsoleInputStream/NativeMethods.cs (+31 -0)
tools/ReadConsoleInputStream/Program.cs (+147 -0)
tools/ReadConsoleInputStream/ReadConsoleInputStream.cs (+198 -0)
tools/ReadConsoleInputStream/ReadConsoleInputStreamDemo.csproj (+15 -0)
tools/ReadConsoleInputStream/ReadConsoleInputStreamDemo.sln (+25 -0)

📄 Description

This is a demo that shows how we can have a stream-oriented view of characters from the console
while also listening to console events like mouse, menu, focus, buffer/viewport(1) resize events.

This has always been tricky to do because ReadConsoleW/A doesn't allow retrieving events.
Only ReadConsoleInputW/A returns events, but isn't stream-oriented. Using both doesn't work because
ReadConsoleW/A flushes the input queue, meaning calls to ReadConsoleInputW/A will wait forever.
I do this by deriving a new Stream class which wraps ReadConsoleInputW and accepts a provider/consumer implementation of BlockingCollection<Kernel32.INPUT_RECORD>. This allows asynchronous monitoring of console events while simultaneously streaming the character input. I also use Mark Gravell's great System.IO.Pipelines utility classes (2) and David Hall's excellent P/Invoke wrappers (3) to make this demo cleaner to read; both are pulled from NuGet.

(1) in versions of windows 10 prior to 1809, the buffer resize event only fires for enlarging
the viewport, as this would cause the buffer to be enlarged too. Now it fires even when
shrinking the viewport, which won't change the buffer size.

(2) https://github.com/mgravell/Pipelines.Sockets.Unofficial
https://www.nuget.org/packages/Pipelines.Sockets.Unofficial

(3) https://github.com/dahall/Vanara
https://www.nuget.org/packages/Vanara.Pinvoke.Kernel32

Oisin Grehan - 2019/4/21
https://twitter.com/oising
https://github.com/oising


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/microsoft/terminal/pull/414 **Author:** [@oising](https://github.com/oising) **Created:** 4/21/2019 **Status:** ✅ Merged **Merged:** 4/25/2019 **Merged by:** [@miniksa](https://github.com/miniksa) **Base:** `master` ← **Head:** `readconsoleinputstream-demo` --- ### 📝 Commits (5) - [`cade139`](https://github.com/microsoft/terminal/commit/cade139e0c3eaae2b6486704b61b765f32a0c11b) initial commit for tools/readconsoleinputstream PR - [`7eea98d`](https://github.com/microsoft/terminal/commit/7eea98d4ea484479fd6ba4afce7485564851bba8) add sln file - [`2dc178b`](https://github.com/microsoft/terminal/commit/2dc178b852b1ed7fecea1ce9132b44ec3a44e28b) address issues and suggestions in PR review - [`5bd3f88`](https://github.com/microsoft/terminal/commit/5bd3f887b1b14290fba892685cb42f3e2f50f950) remove empty xmldoc - [`cfe3eb9`](https://github.com/microsoft/terminal/commit/cfe3eb9624352ac5eb3f1c7f7a5dad2778618979) address PR comments (root namespace) ### 📊 Changes **6 files changed** (+474 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `tools/ReadConsoleInputStream/ConcurrentBoundedQueue.cs` (+58 -0) ➕ `tools/ReadConsoleInputStream/NativeMethods.cs` (+31 -0) ➕ `tools/ReadConsoleInputStream/Program.cs` (+147 -0) ➕ `tools/ReadConsoleInputStream/ReadConsoleInputStream.cs` (+198 -0) ➕ `tools/ReadConsoleInputStream/ReadConsoleInputStreamDemo.csproj` (+15 -0) ➕ `tools/ReadConsoleInputStream/ReadConsoleInputStreamDemo.sln` (+25 -0) </details> ### 📄 Description This is a demo that shows how we can have a stream-oriented view of characters from the console while also listening to console events like mouse, menu, focus, buffer/viewport(1) resize events. This has always been tricky to do because ReadConsoleW/A doesn't allow retrieving events. Only ReadConsoleInputW/A returns events, but isn't stream-oriented. Using both doesn't work because ReadConsoleW/A flushes the input queue, meaning calls to ReadConsoleInputW/A will wait forever. I do this by deriving a new Stream class which wraps ReadConsoleInputW and accepts a provider/consumer implementation of BlockingCollection<Kernel32.INPUT_RECORD>. This allows asynchronous monitoring of console events while simultaneously streaming the character input. I also use Mark Gravell's great System.IO.Pipelines utility classes (2) and David Hall's excellent P/Invoke wrappers (3) to make this demo cleaner to read; both are pulled from NuGet. (1) in versions of windows 10 prior to 1809, the buffer resize event only fires for enlarging the viewport, as this would cause the buffer to be enlarged too. Now it fires even when shrinking the viewport, which won't change the buffer size. (2) https://github.com/mgravell/Pipelines.Sockets.Unofficial https://www.nuget.org/packages/Pipelines.Sockets.Unofficial (3) https://github.com/dahall/Vanara https://www.nuget.org/packages/Vanara.Pinvoke.Kernel32 Oisin Grehan - 2019/4/21 https://twitter.com/oising https://github.com/oising --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-31 09:01:26 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#24108