C# - BlockingCollection

Markus Freitag 3,786 Reputation points
2020-12-17T13:52:04.11+00:00

Hello,

public static async Task BC_AddTakeCompleteAdding()  
    {  
        using (BlockingCollection<int> bc = new BlockingCollection<int>())  
        {  
            // Spin up a Task to populate the BlockingCollection  
            Task t1 = Task.Run(() =>  
            {  
                bc.Add(1);  
                bc.Add(2);  
                bc.Add(3);  
                bc.CompleteAdding();  
            });  
  
            // Spin up a Task to consume the BlockingCollection  
            Task t2 = Task.Run(() =>  
            {  
                try  
                {  
                    // Consume consume the BlockingCollection  
                    while (true) Console.WriteLine(bc.Take());  
                }  
                catch (InvalidOperationException)  
                {  
                    // An InvalidOperationException means that Take() was called on a completed collection  
                    Console.WriteLine("That's All!");  
                }  
            });  
  
            await Task.WhenAll(t1, t2);  
        }  
    }  

Is there a way to enable a LastIn FirstOut method directly in this list?
Background:
I could use this list.
But I always have to take out the last element first.

Thanks.

blockingcollection-overview

------

When I use a C#WPF application for the test examples.

  public void BtnBlockingList_Click(object sender, RoutedEventArgs e)  
        {  
            Test();          
        }  
  
        static async Task Test()  
        {  
            await AddTakeDemo.BC_AddTakeCompleteAdding();  
            TryTakeDemo.BC_TryTake();  
            FromToAnyDemo.BC_FromToAny();  
            await ConsumingEnumerableDemo.BC_GetConsumingEnumerable();  
            Console.WriteLine("Press any key to exit.");  
            Console.ReadKey();  
        }  

49213--2-blo.png

What is the correct syntax?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,927 questions
0 comments No comments
{count} votes

Accepted answer
  1. YASER SHADMEHR 781 Reputation points
    2020-12-17T17:57:29.727+00:00

    First-Q

    When you create a BlockingCollection<T>, you can specify not only the bounded capacity but also the type of collection to use. You can use any collection class that implements the IProducerConsumerCollection<T> interface for example, for LIFO:

    BlockingCollection<int> bc = new BlockingCollection<int>(new ConcurrentStack<int>());
    

    Second-Q

    You need to add async keyword to the signature of BtnBlockingList_ClickAsync method and call Test() method with await.

    public async Task BtnBlockingList_ClickAsync(object sender, RoutedEventArgs e)
            {
                await Test();
            }
    
            static async Task Test()
            {
                await AddTakeDemo.BC_AddTakeCompleteAdding();
                TryTakeDemo.BC_TryTake();
                FromToAnyDemo.BC_FromToAny();
                await ConsumingEnumerableDemo.BC_GetConsumingEnumerable();
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
    
    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.