Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6565993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:09:50+00:00 2026-05-25T14:09:50+00:00

If I have a ConcurrentQueue, is there a preferred way to consume it with

  • 0

If I have a ConcurrentQueue, is there a preferred way to consume it with a Linq statement? It doesn’t have a method to dequeue all the items as a sequence, and it’s enumerator doesn’t remove items.

I’m doing batch consumption, meaning periodically I want to process the queue and empty it, instead of processing it until it is empty and blocking until more items are enqueued. BlockingCollection doesn’t seem like it will work because it will block when it gets to the last item, and I want that thread to do other stuff, like clear other queues.

static ConcurrentQueue<int> MyQueue = new ConcurrentQueue<int>();
void Main()
{
    MyQueue.Enqueue(1);MyQueue.Enqueue(2);MyQueue.Enqueue(3);MyQueue.Enqueue(4);MyQueue.Enqueue(5);

    var lst = MyQueue.ToLookup(x => x.SomeProperty);
    //queue still has all elements
    MyQueue.Dump("queue");  
}

For now, I’ve made a helper method

static IEnumerable<T> ReadAndEmptyQueue<T>(this ConcurrentQueue<T> q)
{
    T item;
    while(q.TryDequeue(out item))
    {
        yield return item;
    }
}

var lk = MyQueue.ReadAndEmptyQueue().ToLookup(x => x.SomeProperty);
MyQueue.Dump(); //size is now zero

Is there a better way, or am I doing it right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:09:51+00:00Added an answer on May 25, 2026 at 2:09 pm

    Your approach is very reasonable, in my opinion. Allowing a consumer to empty the queue in this fashion is clean and simple.

    BlockingCollection doesn’t seem like it will work because it will block when it gets to the last item, and I want that thread to do other stuff, like clear other queues.

    The one thing I’d mention – sometimes, from a design standpoint, it’s easier to just fire off a separate consumer thread per queue. If you do that, each BlockingCollection<T> can just use GetConsumingEnumerable() and block as needed, as they’ll be in a wait state when the queue is empty.

    This is the approach I take more frequently, as it’s often much simpler from a synchronization standpoint if each collection has one or more dedicated consumers, instead of a consumer switching between what it’s consuming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that has a ConcurrentQueue of items that have an ID
I have multiple threads each one with its own private concurrent queue and all
Have you ever seen any of there error messages? -- SQL Server 2000 Could
The System.Threading.ConcurrentQueue.TryDequeue method threw an exception the other day that took me totally by
I have the following PLINQ statement in a C# program: foreach (ArrestRecord arrest in
So I have something along the lines of this private List<ConcurrentQueue<int>> listOfQueues = new
I have a database-synchronisation task that takes some time to process, as there are
Have this method call: -> simpleJdbcTemplate.queryForInt(SQL,null); -> queryForInt() method in the springs SimpleJdbcTemplate throws
I have a ConcurrentQueue that is being populated with an arbitrary number of objects,
i have built a Producer Consumer queue wrapping a ConcurrentQueue of .net 4.0 with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.