Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Threading.Channels.ChannelWriter: practical reference

System.Threading.Channels.ChannelWriter: practical reference: ChannelWriter<T> writes elements and completes the channel through TryComplete or Complete.

Version: .NET API; verified against .NET 10 reference surface

Syntax

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

ChannelWriter writes elements and completes the channel through TryComplete or Complete.

Use TryWrite for a fast attempt or WriteAsync when backpressure is required.

Further writes are forbidden after completion; only one component should own completion.

TryWrite can return false for a full or closed channel.

The complete program actually uses System.Threading.Channels.ChannelWriter; its exact expected stdout is 7.

Examples

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());

Result

7

The complete program actually uses `System.Threading.Channels.ChannelWriter`; its exact expected stdout is `7`.

Sources