Skip to content

Commit feaaa29

Browse files
committedMar 18, 2025·
Channel.CreateUnboundedPrioritized Method
1 parent 6ad553f commit feaaa29

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

‎CreateUnboundedPrioritized/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Channel.CreateUnboundedPrioritized Method
2+
// https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels.channel.createunboundedprioritized?view=net-9.0
3+
4+
// [API Proposal] Channel.CreateUnboundedPrioritized #62761
5+
// https://github.com/dotnet/runtime/issues/62761
6+
7+
// Concurrent collection with priority
8+
// https://stackoverflow.com/questions/23470196/concurrent-collection-with-priority
9+
10+
using System.Threading.Channels;
11+
12+
var channel = Channel.CreateUnboundedPrioritized<(int priority, string message)>();
13+
14+
// Write sample messages
15+
await channel.Writer.WriteAsync((2, "Emergency service dispatch"));
16+
await channel.Writer.WriteAsync((1, "Routine check: Blood pressure measurement"));
17+
await channel.Writer.WriteAsync((3, "Heart attack alert!"));
18+
await channel.Writer.WriteAsync((3, "Fire alarm!"));
19+
20+
channel.Writer.Complete();
21+
22+
// Read and process messages based on priority
23+
await foreach (var item in channel.Reader.ReadAllAsync())
24+
{
25+
Console.WriteLine($"Processing: {item.message} (Priority: {item.priority})");
26+
}

‎CsharpLangExamples.sln

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Roslyn Analyzer", "Roslyn A
9595
EndProject
9696
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewLINQMethodsCsharp10", "NewLINQMethodsCsharp10\NewLINQMethodsCsharp10.csproj", "{D7C23C3C-8B11-4E31-830F-17CD9670D49E}"
9797
EndProject
98+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CreateUnboundedPrioritized", "CreateUnboundedPrioritized\CreateUnboundedPrioritized.csproj", "{8D54CE39-9351-4F21-B5A2-1238973E63B3}"
99+
EndProject
98100
Global
99101
GlobalSection(SolutionConfigurationPlatforms) = preSolution
100102
Debug|Any CPU = Debug|Any CPU
@@ -277,6 +279,10 @@ Global
277279
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Debug|Any CPU.Build.0 = Debug|Any CPU
278280
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Release|Any CPU.ActiveCfg = Release|Any CPU
279281
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Release|Any CPU.Build.0 = Release|Any CPU
282+
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
283+
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
284+
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
285+
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Release|Any CPU.Build.0 = Release|Any CPU
280286
EndGlobalSection
281287
GlobalSection(SolutionProperties) = preSolution
282288
HideSolutionNode = FALSE

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Examples of C# programming, to track and stay up-to-date with the latest languag
2020
* `Index()`
2121
* `CountBy()`
2222
* `AggregateBy()`
23-
23+
* [CreateUnboundedPrioritized](CreateUnboundedPrioritized/Program.cs)
24+
2425
* C# 12 is supported on .NET 8 (November, 2023)
2526
* [Primary constructors](PrimaryConstructors/Program.cs)
2627
* [Default values for parameters in lambda expressions](DefaultLambdaParameters/Program.cs)

0 commit comments

Comments
 (0)