Skip to content

Commit

Permalink
AsynchronousStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimatay committed Aug 18, 2024
1 parent 8e6e88c commit c6840be
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion AsynchronousStreams/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;

namespace AsynchronousStreams
{
Expand All @@ -15,14 +16,37 @@ namespace AsynchronousStreams
class Program
{
static async Task Main(string[] args)
{
//await AsynchronousStreamsWithCancellationToken();
await PrintSequence();
}

static async IAsyncEnumerable<int> 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))
{
Console.WriteLine(result);
}
}

static async IAsyncEnumerable<string> GetTopSearchResults(string term, [EnumeratorCancellation]CancellationToken token = default)
{
while (!token.IsCancellationRequested)
Expand Down

0 comments on commit c6840be

Please sign in to comment.