Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel.CreateUnboundedPrioritized Method #1

Merged
merged 2 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CreateUnboundedPrioritized/CreateUnboundedPrioritized.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
26 changes: 26 additions & 0 deletions CreateUnboundedPrioritized/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Channel.CreateUnboundedPrioritized Method
// https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels.channel.createunboundedprioritized?view=net-9.0

// [API Proposal] Channel.CreateUnboundedPrioritized #62761
// https://github.com/dotnet/runtime/issues/62761

// Concurrent collection with priority
// https://stackoverflow.com/questions/23470196/concurrent-collection-with-priority

using System.Threading.Channels;

var channel = Channel.CreateUnboundedPrioritized<(int priority, string message)>();

// Write sample messages
await channel.Writer.WriteAsync((2, "Emergency service dispatch"));
await channel.Writer.WriteAsync((1, "Routine check: Blood pressure measurement"));
await channel.Writer.WriteAsync((3, "Heart attack alert!"));
await channel.Writer.WriteAsync((3, "Fire alarm!"));

channel.Writer.Complete();

// Read and process messages based on priority
await foreach (var item in channel.Reader.ReadAllAsync())
{
Console.WriteLine($"Processing: {item.message} (Priority: {item.priority})");
}
12 changes: 12 additions & 0 deletions CsharpLangExamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoslynAnalyzer.Tests", "Ros
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Roslyn Analyzer", "Roslyn Analyzer", "{A20B336B-C5A8-46EC-A4D3-843C257A4809}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewLINQMethodsCsharp10", "NewLINQMethodsCsharp10\NewLINQMethodsCsharp10.csproj", "{D7C23C3C-8B11-4E31-830F-17CD9670D49E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CreateUnboundedPrioritized", "CreateUnboundedPrioritized\CreateUnboundedPrioritized.csproj", "{8D54CE39-9351-4F21-B5A2-1238973E63B3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -271,6 +275,14 @@ Global
{9C9BBC19-CDBA-4851-B3D8-8C383F4742FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C9BBC19-CDBA-4851-B3D8-8C383F4742FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C9BBC19-CDBA-4851-B3D8-8C383F4742FE}.Release|Any CPU.Build.0 = Release|Any CPU
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Release|Any CPU.Build.0 = Release|Any CPU
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 10 additions & 0 deletions NewLINQMethodsCsharp10/NewLINQMethodsCsharp10.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
42 changes: 42 additions & 0 deletions NewLINQMethodsCsharp10/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

// Chunk.cs
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Chunk.cs

// how do I chunk an enumerable?
// https://stackoverflow.com/questions/12389203/how-do-i-chunk-an-enumerable

var movies = new List<Movie>
{
new Movie("Titanic", 1998, 4.5f),
new Movie("The Fifth Element", 1997, 4.6f),
new Movie("Terminator 2", 1991, 4.7f),
new Movie("Avatar", 2009, 5),
new Movie("Platoon", 1986, 4),
new Movie("My Neighbor Totoro", 1988, 5)
};

Console.WriteLine("---------Chunk-----------");

// Chunk
foreach (var movie in movies.Where(movie => movie.Rating > 4.5f).Chunk(3))
{
Console.WriteLine(string.Join(",", movies.Select(currentMovie => currentMovie.Name)));
}

Console.WriteLine("---------DistinctBy-----------");

// DistinctBy
foreach (var movie in movies.DistinctBy(movie => movie.Rating))
{
Console.WriteLine(string.Join(",", movies.Select(currentMovie => currentMovie.Name)));
}

Console.WriteLine("---------Take-----------");

// Take with Ranges
foreach (var movie in movies.Take(^5..3))
{
Console.WriteLine(string.Join(",", movies.Select(currentMovie => currentMovie.Name)));
}

record Movie(string Name, int Year, float Rating);
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Examples of C# programming, to track and stay up-to-date with the latest languag
* `Index()`
* `CountBy()`
* `AggregateBy()`

* [CreateUnboundedPrioritized](CreateUnboundedPrioritized/Program.cs)

* C# 12 is supported on .NET 8 (November, 2023)
* [Primary constructors](PrimaryConstructors/Program.cs)
* [Default values for parameters in lambda expressions](DefaultLambdaParameters/Program.cs)
Expand All @@ -35,6 +36,11 @@ Examples of C# programming, to track and stay up-to-date with the latest languag

* C# 10 is supported on .NET 6 (November, 2021)
* [Global using directive](GlobalUsingDirective/Program.cs)
* API Improvements
* [New LINQ Methods](NewLINQMethodsCsharp10/Program.cs)
* `Chunk()`
* `DistinctBy()`
* `Take()`

* C# 9 is supported on .NET 5 (November, 2020)
* [Target-typed new expressions ](TargetTypedNewExpressions/Program.cs)
Expand Down
Loading