© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
1 reply
Tandy

❔ Does BlockingCollection TryTake free the consuming thread to do other things?

I'm not too knowledgeable about multi-threading. But I'm trying to use
BlockingCollection.TryTake
BlockingCollection.TryTake
to consume messages like so:

Task.Run(async () =>
{
    while (!_queue.IsCompleted)
    {
        _queue.TryTake(out var command, Timeout.Infinite);
    }
});
Task.Run(async () =>
{
    while (!_queue.IsCompleted)
    {
        _queue.TryTake(out var command, Timeout.Infinite);
    }
});


While
_queue.TryTake
_queue.TryTake
is waiting for an item - is it occupying the thread such that other API functions will be impacted? My fear is that I have to create n number of these collections. If each one is permanently blocking a thread, then the server will run out of resources. But if the thread is freed to do other things - like process incoming HTTP requests - before being signaled then I should be safe.

The documentation is a little confusing, but I'm hopeful that the "blocked thread" is free to do other work:

The thread is then free to do some other useful work before trying again to access the collection.

https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/blockingcollection-overview#timed-blocking-operations
BlockingCollection Overview
Read about BlockingCollection, a thread-safe collection class in .NET. This class offers features like concurrent adding & taking of items from many threads.
BlockingCollection Overview
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ How to pause BlockingCollection consumption?
C#CC# / help
4y ago
How things get to the place
C#CC# / help
2y ago
❔ ✅ Why does this block the UI thread?
C#CC# / help
3y ago
✅ How does the button know what to do?
C#CC# / help
17mo ago