Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C#

System.Threading.Channels.ChannelReader: referência prática

System.Threading.Channels.ChannelReader: referência prática: ChannelReader<T> lê elementos de forma assíncrona e sinaliza a conclusão do canal.

Versão: .NET API; verified against .NET 10 reference surface

Sintaxe

System.Threading.Channels.ChannelReader /* select a documented member or constructor */

ChannelReader lê elementos de forma assíncrona e sinaliza a conclusão do canal.

Usa ReadAllAsync ou um ciclo WaitToReadAsync/TryRead para um consumidor eficiente.

Um resultado true de WaitToReadAsync não garante um elemento quando vários leitores competem.

ReadAsync num canal vazio e concluído lança ChannelClosedException.

O programa completo usa realmente System.Threading.Channels.ChannelReader; o stdout exato esperado é 6.

Exemplos

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

O programa completo usa realmente `System.Threading.Channels.ChannelReader`; o stdout exato esperado é `6`.

Fontes