I write the code bellow and forgot to declare Main method as async, so my guess was that the producer
and consumer
tasks should run synchronously.
using System.Collections.Concurrent;
namespace MainProgram;
public class Program
{
public static void Main(string[] args)
{
using var intCollection = new BlockingCollection<int>();
int sum = 0;
Task.Run(() =>
{
Console.WriteLine("Running Producer");
Thread.Sleep(1000);
for (int i = 0; i < 10; i++)
{
intCollection.Add(i);
Console.WriteLine(i);
}
intCollection.CompleteAdding();
});
Task.Run(() =>
{
Console.WriteLine("Running Consumer");
Thread.Sleep(1000);
while (!intCollection.IsCompleted)
{
sum += intCollection.Take();
Console.WriteLine("Thingi Thingi");
}
});
Console.WriteLine("aaabbb");
Console.ReadLine();
}
}
But the output of this code looks like this:
aaabbb
Running Producer
Running Consumer
0
Thingi Thingi
1
Thingi Thingi
2
Thingi Thingi
3
Thingi Thingi
4
Thingi Thingi
5
Thingi Thingi
6
Thingi Thingi
7
8
9
Thingi Thingi
Thingi Thingi
Thingi Thingi
This is to my surprise, because the tasks appear to run asynchronously. Can someone explain this to me?