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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:34:49+00:00 2026-05-21T03:34:49+00:00

I’m writing a relatively simple proxy app for a web service. The general idea

  • 0

I’m writing a relatively simple “proxy” app for a web service. The general idea is that a TCP server (w/asynchronous connections) will read (string) data from clients and put that data (as part of the read callback function) into one of two queues (Q1 and Q2). Another thread will read the data in these queues and pass it to the web service. Data in Q1 should take priority over any data that might be in Q2.

I’ve been reading about the producer/consumer pattern and it seems like that’s pretty much what I’m trying to implement with regard to the queues. Since my enqueue and dequeue operations will be happening on different threads, it seems obvious that my queues will need to be thread safe and support some kind of locking mechanism? This is a .NET 4.0 application and I saw the docs on the new BlockingCollection and ConcurrentQueue classes but I’m not sure exactly what the difference is or how I’d implement them in this scenario. Can anyone shed some more light on that? Thank you!

  • 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-21T03:34:50+00:00Added an answer on May 21, 2026 at 3:34 am

    I would do it like the following class. You call Enqueue() when you produce an item to add it to one of the queues. This method always returns (almost) immediately. In another thread, you call Dequeue() when you are ready to consume an item. It tries to first take from high priority queue. If no items are available at the moment in any of the queues, the call blocks. When you finished producing, you call Complete(). After that call has been made and both queues are empty, the next call (or the current blocked call) to Dequeue() throws InvalidOperationException.

    If your producer(s) can be faster than your consumer(s) for long periods of time, you should bound the queues (new BlockingCollection<T>(capacity)). But in this case, if you have only one thread that produces both low and high priority items, it’s possible that high priority items would have to wait for low priority items. You could fix this by having one thread for producing high priority items and one for low priority items. Or you could bound only high priority queue and hope that you won’t get one million low priority items at once.

    class Worker<T>
    {
        BlockingCollection<T> m_highPriorityQueue = new BlockingCollection<T>();
        BlockingCollection<T> m_lowPriorityQueue = new BlockingCollection<T>();
    
        public void Enqueue(T item, bool highPriority)
        {
            BlockingCollection<T> queue;
            if (highPriority)
                queue = m_highPriorityQueue;
            else
                queue = m_lowPriorityQueue;
    
            queue.Add(item);
        }
    
        public T Dequeue()
        {
            T result;
    
            if (!m_highPriorityQueue.IsCompleted)
            {
                if (m_highPriorityQueue.TryTake(out result))
                    return result;
            }
    
            if (!m_lowPriorityQueue.IsCompleted)
            {
                if (m_lowPriorityQueue.TryTake(out result))
                    return result;
            }
    
            if (m_highPriorityQueue.IsCompleted && m_lowPriorityQueue.IsCompleted)
                throw new InvalidOperationException("All work is done.");
            else
            {
                try
                {
                    BlockingCollection<T>.TakeFromAny(
                        new[] { m_highPriorityQueue, m_lowPriorityQueue },
                        out result);
                }
                catch (ArgumentException ex)
                {
                    throw new InvalidOperationException("All work is done.", ex);
                }
    
                return result;
            }
        }
    
        public void Complete()
        {
            m_highPriorityQueue.CompleteAdding();
            m_lowPriorityQueue.CompleteAdding();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am writing an app with both english and french support. The app requests
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.