CKTIS / C#
System.Threading.Channels.ChannelReader: referencia práctica
System.Threading.Channels.ChannelReader: referencia práctica: ChannelReader<T> lee elementos de forma asíncrona y señala la finalización del canal.
Versión: .NET API; verified against .NET 10 reference surface
Sintaxis
System.Threading.Channels.ChannelReader /* select a documented member or constructor */ChannelReader lee elementos de forma asíncrona y señala la finalización del canal.
Usa ReadAllAsync o un bucle WaitToReadAsync/TryRead para un consumidor eficiente.
Un resultado true de WaitToReadAsync no garantiza un elemento si compiten varios lectores.
ReadAsync en un canal vacío y completado lanza ChannelClosedException.
El programa completo usa realmente System.Threading.Channels.ChannelReader; su stdout esperado exacto es 6.
Ejemplos
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
var channel = Channel.CreateUnbounded<int>(); channel.Writer.TryWrite(6);
ChannelReader<int> reader = channel.Reader;
Console.WriteLine(reader.TryRead(out var value) ? value : -1);
Resultado
6
El programa completo usa realmente `System.Threading.Channels.ChannelReader`; su stdout esperado exacto es `6`.