ToAsyncEnumerable()

  • Thread starter Thread starter BenTam
  • Start date Start date
B

BenTam

Guest
Dear All,

I copied code seqments from the book "Introduction to CSharp 8" into my VS Code Editor. Then added necessary "using" to it. The method "ToAsyncEnumerable ()" was red lined. Could anybody help?


1616716.gif

Source code

static async Task ConsumeAsyncSumSeqeunc (IAsyncEnumerable <int> sequence){
ConsoleExt.WriteLineAsync("ConsumeAsyncSumSeqeunc Called");
await foreach (var value in sequence)
{
ConsoleExt.WriteLineAsync($"Consuming the value: {value}");
// Simulate some delay!
Task.Delay(TimeSpan.FromSeconds(1)).Wait();
}
}
static async IAsyncEnumerable<int> ProduceAsyncSumSeqeunc(int count)
{
ConsoleExt.WriteLineAsync("ProduceAsyncSumSeqeunc Called");
var sum = 0;
for (var i = 0; i <= count; i++)
{
sum = sum + i;
//ConsoleExt.WriteLineAsync("Producing the value:" + i);
// Simulate some delay!
await Task.Delay(TimeSpan.FromSeconds(0.5));
yield return sum;
}
}

const int count = 5;
ConsoleExt.WriteLine("Starting Async Streams Demo!");
// Start a new task to produce an async sequence of data!
IAsyncEnumerable<int> pullBasedAsyncSequence = ProduceAsyncSumSeqeunc(count).ToAsyncEnumerable();ConsoleExt.WriteLineAsync ("X # X # X # X Doing some other work X # X # X # X");// Start another task to consume the async data sequence!var consumingTask = Task.Run (() => ConsumeAsyncSumSeqeunc (pullBasedAsyncSequence));// Just for the demo! Wait until the task is finished!consumingTask.Wait ();ConsoleExt.WriteLineAsync ("Async Streams Demo Done!");

Continue reading...
 
Back
Top