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

  • Home
  • SEARCH
  • 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 842225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:55:30+00:00 2026-05-15T05:55:30+00:00

I am using the parallel data structures in my .NET 4 application and I

  • 0

I am using the parallel data structures in my .NET 4 application and I have a ConcurrentQueue that gets added to while I am processing through it.

I want to do something like:

personqueue.AsParallel().WithDegreeOfParallelism(20).ForAll(i => ... );

as I make database calls to save the data, so I am limiting the number of concurrent threads.

But, I expect that the ForAll isn’t going to dequeue, and I am concerned about just doing

ForAll(i => {
    personqueue.personqueue.TryDequeue(...);
    ...
});

as there is no guarantee that I am popping off the correct one.

So, how can I iterate through the collection and dequeue, in a parallel fashion.

Or, would it be better to use PLINQ to do this processing, in parallel?

  • 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-15T05:55:31+00:00Added an answer on May 15, 2026 at 5:55 am

    Well I’m not 100% sure what you try to archive here. Are you trying to just dequeue all items until nothing is left? Or just dequeue lots of items in one go?

    The first probably unexpected behavior starts with this statement:

     theQueue.AsParallel()
    

    For a ConcurrentQueue, you get a ‘Snapshot’-Enumerator. So when you iterate over a concurrent stack, you only iterate over the snapshot, no the ‘live’ queue.

    In general I think it’s not a good idea to iterate over something you’re changing during the iteration.

    So another solution would look like this:

            // this way it's more clear, that we only deque for theQueue.Count items
            // However after this, the queue is probably not empty
            // or maybe the queue is also empty earlier   
            Parallel.For(0, theQueue.Count,
                         new ParallelOptions() {MaxDegreeOfParallelism = 20},
                         () => { 
                             theQueue.TryDequeue(); //and stuff
                         });
    

    This avoids manipulation something while iterating over it. However, after that statement, the queue can still contain data, which was added during the for-loop.

    To get the queue empty for moment in time you probably need a little more work. Here’s an really ugly solution. While the queue has still items, create new tasks. Each task start do dequeue from the queue as long as it can. At the end, we wait for all tasks to end. To limit the parallelism, we never create more than 20-tasks.

            // Probably a kitty died because of this ugly code ;)
            // However, this code tries to get the queue empty in a very aggressive way
            Action consumeFromQueue = () =>
                                          {
                                              while (tt.TryDequeue())
                                              {
                                                  ; // do your stuff
                                              }
                                          };
            var allRunningTasks = new Task[MaxParallism];
            for(int i=0;i<MaxParallism && tt.Count>0;i++)
            {
                allRunningTasks[i] = Task.Factory.StartNew(consumeFromQueue);  
            }
            Task.WaitAll(allRunningTasks);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The form to change it is at site information: admin/settings/site-information. May 15, 2026 at 3:16 pm
  • Editorial Team
    Editorial Team added an answer I am assuming that you are not looking for a… May 15, 2026 at 3:16 pm
  • Editorial Team
    Editorial Team added an answer try to raise perm space, add following parameters to vm… May 15, 2026 at 3:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.