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

CKTIS / C#

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

System.Threading.Channels.ChannelWriter: referência prática: ChannelWriter<T> escreve elementos e conclui o canal através de TryComplete ou Complete.

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

Sintaxe

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

ChannelWriter escreve elementos e conclui o canal através de TryComplete ou Complete.

Usa TryWrite para uma tentativa rápida ou WriteAsync quando for preciso backpressure.

Depois da conclusão não são permitidas mais escritas; só um componente deve controlar a conclusão.

TryWrite pode devolver false para um canal cheio ou fechado.

O programa completo usa realmente System.Threading.Channels.ChannelWriter; o stdout exato esperado é 7.

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>();
ChannelWriter<int> writer = channel.Writer; writer.TryWrite(7);
Console.WriteLine(await channel.Reader.ReadAsync());

Resultado

7

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

Fontes