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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:34:16+00:00 2026-05-20T17:34:16+00:00

What is the best way of getting messages from many threads onto a queue

  • 0

What is the best way of getting messages from many threads onto a queue and have a separate thread processing items of this queue one at a time?

I am frequently using this pattern when trying to disconnect activities from many threads.

I am using a BlockingCollection for this as shown in a code extract below:

// start this task in a static constructor
Task.Factory.StartNew(() => ProcessMultiUseQueueEntries(), TaskCreationOptions.LongRunning);


private static BlockingCollection<Tuple<XClientMsgExt, BOInfo, string, BOStatus>> _q = new BlockingCollection<Tuple<XClientMsgExt, BOInfo, string, BOStatus>>();

    /// <summary>
    /// queued - Simple mechanism that will log the fact that this user is sending an xMsg (FROM a user)
    /// </summary>
    public static void LogXMsgFromUser(XClientMsgExt xMsg)
    {
        _q.Add(new Tuple<XClientMsgExt, BOInfo, string, BOStatus>(xMsg, null, "", BOStatus.Ignore));
    }

    /// <summary>
    /// queued - Simple mechanism that will log the data being executed by this user
    /// </summary>
    public static void LogBOToUser(BOInfo boInfo)
    {
        _q.Add(new Tuple<XClientMsgExt, BOInfo, string, BOStatus>(null, boInfo, "", BOStatus.Ignore));
    }

    /// <summary>
    /// queued - Simple mechanism that will log the status of the BO being executed by this user (causes the red square to flash)
    /// </summary>
    public static void LogBOStatus(string UserID, BOStatus status)
    {
        _q.Add(new Tuple<XClientMsgExt, BOInfo, string, BOStatus>(null, null, UserID, status));
    }

    /// <summary>
    /// An endless thread that will keep checking the Queue for new entrants.
    /// NOTE - no error handling since this can't fail... :) lol etc 
    /// </summary>
    private static void ProcessMultiUseQueueEntries()
    {
        while (true)        //  eternal loop
        {
            Tuple<XClientMsgExt, BOInfo, string, BOStatus> tuple = _q.Take();

            // Do stuff

        }
    }

This works fine – so I thought – until the Performance Wizard in VS2010 started to highlight the _q.Take() row as the highest contention line in my code!

Note I have also used a standard ConcurrentQueue with a ManualResetEvent combination and each time I insert an item onto the queue I signal the resetevent allowing the worker thread to examine and process the Queue but this also had the same net effect of being highlighted on the .WaitOne() method…

Are there other ways of solving this common pattern of having many threads adding objects into a concurrent queue – and have a single thread ploughing its way through the items one at a time and in its own time…

Thanks!!

  • 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-20T17:34:17+00:00Added an answer on May 20, 2026 at 5:34 pm

    Highest contention line? Yes, because it is a blocking collection! That call will block (e.g., it might be waiting on a WaitHandle) until another element is added to the collection.

    Are you sure this is a problem? It sounds like exactly what I’d expect.

    In case it isn’t clear what I mean, consider this code:

    var blocker = new BlockingCollection<int>();
    int nextItem = blocker.Take();
    

    How long would you expect that Take call above to run? I’d expect it to wait forever because nothing’s being added to blocker. Thus if I profiled the “performance” of the above code, I’d see Take right up at the top of the list of long-running methods. But this would be not be indicative of a problem; again: you want that call to block.


    On a completely separate note, might I recommend replacing Tuple<XClientMsgExt, BOInfo, string, BOStatus> with a type whose properties have descriptive names? (This suggestion has nothing to do with your question, of course; it’s just a piece of general advice.)

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

Sidebar

Related Questions

What is the best way(s) to keep the User from getting confused by this
The best way of describing this is I have a table of people with
An activity I have starts an AsyncTask. What is the best way of getting
What's the best way of getting input from appwidget . I know that I
I am trying to figure out the best way of getting the record count
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
What is the best way to view the HTTP response headers from a request
What is the best way of getting the row that was just added, I
Is there a reliable way of getting access token errors from the facebook platform?

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.