From the course: Advanced C#: Thread-Safe Data with Concurrent Collections

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

Use the CompleteAdding method

Use the CompleteAdding method

- [Illustrator] So far, in this example, I have one thread that's producing the items and another thread that's consuming them. In a real world application, you wouldn't write code like this, we have a while loop that's running indefinitely, you'd have certain conditions where you'd want to stop producing the items for the blocking collection. It could be because you only processed the data certain hours of the day, it could be when you get enough exceptions thrown that something's wrong with your blocking collection and you want to stop. It could be that the consumer is slowing down and can't process the items and use timed out enough times that you decide you want to stop. Whatever the reason, there has to be a way for the producer to say, I'm finished. So that's what we're looking at in this video. Right now I have to produce items, so I've got this loop. And I'm incrementing the counter and my condition to stop…

Contents