From c6840beb99c9758817560f11d16336e3767acb02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0brahim=20ATAY?= Date: Sun, 18 Aug 2024 11:36:22 +0000 Subject: [PATCH] AsynchronousStreams --- AsynchronousStreams/Program.cs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/AsynchronousStreams/Program.cs b/AsynchronousStreams/Program.cs index 0d714f3..7c4f2ff 100644 --- a/AsynchronousStreams/Program.cs +++ b/AsynchronousStreams/Program.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; +using System.Linq; namespace AsynchronousStreams { @@ -15,6 +16,29 @@ namespace AsynchronousStreams class Program { static async Task Main(string[] args) + { + //await AsynchronousStreamsWithCancellationToken(); + await PrintSequence(); + } + + static async IAsyncEnumerable GenerateSequence() + { + foreach (var item in Enumerable.Range(0, 100)) + { + await Task.Delay(100); + yield return item; + } + } + + static async Task PrintSequence() + { + await foreach (var number in GenerateSequence()) + { + Console.WriteLine(number); + } + } + + static async Task AsynchronousStreamsWithCancellationToken() { var cancellationToken = new CancellationTokenSource(millisecondsDelay: 1000); await foreach (var result in GetTopSearchResults("dotnet").WithCancellation(cancellationToken.Token)) @@ -22,7 +46,7 @@ static async Task Main(string[] args) Console.WriteLine(result); } } - + static async IAsyncEnumerable GetTopSearchResults(string term, [EnumeratorCancellation]CancellationToken token = default) { while (!token.IsCancellationRequested)