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 8055459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:23:34+00:00 2026-06-05T08:23:34+00:00

After doing some research, I’m resorting to any feedback regarding how to effectively remove

  • 0

After doing some research, I’m resorting to any feedback regarding how to effectively remove two items off a Concurrent collection. My situation involves incoming messages over UDP which are currently being placed into a BlockingCollection. Once there are two Users in the collection, I need to safely Take two users and process them. I’ve seen several different techniques including some ideas listed below. My current implementation is below but I’m thinking there’s a cleaner way to do this while ensuring that Users are processed in groups of two. That’s the only restriction in this scenario.

Current Implementation:

    private int userQueueCount = 0;
    public BlockingCollection<User> UserQueue = new BlockingCollection<User>();

    public void JoinQueue(User u)
    {
           UserQueue.Add(u);
           Interlocked.Increment(ref userQueueCount);

           if (userQueueCount > 1)
           {
               IEnumerable<User> users = UserQueue.Take(2);
               if(users.Count==2) {
                 Interlocked.Decrement(ref userQueueCount);
                 Interlocked.Decrement(ref userQueueCount);
                 ... do some work with users but if only one 
                 is removed I'll run into problems
               }

           }
    }

What I would like to do is something like this but I cannot currently test this in a production situation to ensure integrity.

 Parallel.ForEach(UserQueue.Take(2), (u) => { ... });

Or better yet:

    public void JoinQueue(User u)
    {
           UserQueue.Add(u);
           // if needed? increment
           Interlocked.Increment(ref userQueueCount);
           UserQueue.CompleteAdding();
    }

Then implement this somewhere:

        Task.Factory.StartNew(() =>
        {
            while (userQueueCount > 1) OR (UserQueue.Count > 1) If it's safe?
            {
                IEnumerable<User> users = UserQueue.Take(2);
                ... do stuff
            }

        });

The problem with this is that i’m not sure I can guarantee that between the condition (Count > 1) and the Take(2) that i’m ensuring the UserQueue has at least two items to process? Incoming UDP messages are processed in parallel so I need a way to safely pull items off of the Blocking/Concurrent Collection in pairs of two.

Is there a better/safer way to do this?

Revised Comments:
The intented goal of this question is really just to achieve a stable/thread safe method of processing items off of a Concurrent Collection in .Net 4.0. It doesn’t have to be pretty, it just has to be stable in the task of processing items in unordered pairs of twos in a parallel environment.

  • 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-06-05T08:23:36+00:00Added an answer on June 5, 2026 at 8:23 am

    Here is what I’d do in rough Code:

    ConcurrentQueuequeue = new ConcurrentQueue(); //can use a BlockingCollection too (as it's just a blocking ConcurrentQueue by default anyway)
    
    public void OnUserStartedGame(User joiningUser)
    {
       User waitingUser;
       if (this.gameQueue.TryDequeue(out waitingUser)) //if there's someone waiting, we'll get him
          this.MatchUsers(waitingUser, joiningUser);
       else
          this.QueueUser(joiningUser); //it doesn't matter if there's already someone in the queue by now because, well, we are using a queue and it will sort itself out.
    }
    
    private void QueueUser(User user)
    {
       this.gameQueue.Enqueue(user);
    }
    
    private void MatchUsers(User first, User second)
    {
       //not sure what you do here
    }
    

    The basic idea being that if someone’s wants to start a game and there’s someone in your queue, you match them and start a game – if there’s no-one, add them to the queue.
    At best you’ll only have one user in the queue at a time, but if not, well, that’s not too bad either because as other users start games, the waiting ones will gradually removed and no new ones added until the queue is empty again.

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

Sidebar

Related Questions

After doing some research on this issue, I couldn't find any satisfying fix. The
After doing some research on the subject, I've been experimenting a lot with patterns
After doing some research, it would seem that errno 150 occurs when either table
I think it is not complicated but after doing some research I can't find
after doing some research it seems that AppDomains are not really a tool for
After doing some research I heard only PHP 5.3.6+ supports PDO and SSL. I
After doing some research into Dateperiod, it turns out it by default excludes the
After doing some research I need some advice. This is a small project which
After doing some research, everyone seems to advise learning and using some form of
After doing some research, I learned about the postfix notation, and how to parse

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.