Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / C#

System.Threading.Channels.ChannelWriter: referencia práctica

System.Threading.Channels.ChannelWriter: referencia práctica: ChannelWriter<T> escribe elementos y completa el canal mediante TryComplete o Complete.

Versión: .NET API; verified against .NET 10 reference surface

Sintaxis

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

ChannelWriter escribe elementos y completa el canal mediante TryComplete o Complete.

Usa TryWrite para un intento rápido o WriteAsync cuando se necesite backpressure.

Tras completarse no se permiten más escrituras; un solo componente debe ser responsable de completarlo.

TryWrite puede devolver false para un canal lleno o cerrado.

El programa completo usa realmente System.Threading.Channels.ChannelWriter; su stdout esperado exacto es 7.

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

Resultado

7

El programa completo usa realmente `System.Threading.Channels.ChannelWriter`; su stdout esperado exacto es `7`.

Fuentes