Using ConcurrentStack
In this section, we are going to experiment with BlockingCollection<T>
and ConcurrentStack<T>
. In the first example in this chapter, we used BlockingCollection<T>
to read the words that started with a specific letter from the book Ulysses. We are going to make a copy of that project and change the code that reads the lines of text to use ConcurrentStack<T>
inside BlockingCollection<T>
. This will make the lines output in reverse order because a stack uses last in, first out (LIFO) logic. Let’s get started!
- Make a copy of the ParallelExtras.BlockingCollection project from this chapter or modify the existing project if you prefer.
- Open
MainWindow.xaml.cs
and modify theLoadBookLinesFromFile
method to pass a newConcurrentStack<string>
to the constructor ofBlockingCollection<string>
:private async Task<BlockingCollection<string>> LoadBookLinesFromFile() { ...