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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:53:23+00:00 2026-05-27T15:53:23+00:00

I’m building a multithreaded app in .net. I have a thread that listens to

  • 0

I’m building a multithreaded app in .net.

I have a thread that listens to a connection (abstract, serial, tcp…).

When it receives a new message, it adds it to via AddMessage. Which then call startSpool. startSpool checks to see if the spool is already running and if it is, returns, otherwise, starts it in a new thread. The reason for this is, the messages HAVE to be processed serially, FIFO.

So, my questions are…
Am I going about this the right way?
Are there better, faster, cheaper patterns out there?

My apologies if there is a typo in my code, I was having problems copying and pasting.

    ConcurrentQueue<IMyMessage > messages = new ConcurrentQueue<IMyMessage>();

    const int maxSpoolInstances = 1;

    object lcurrentSpoolInstances;
    int currentSpoolInstances = 0;

    Thread spoolThread;

    public void AddMessage(IMyMessage message)
    {
        this.messages.Add(message);

        this.startSpool();
    }

    private void startSpool()
    {
        bool run = false;

        lock (lcurrentSpoolInstances)
        {
            if (currentSpoolInstances <= maxSpoolInstances)
            {
                this.currentSpoolInstances++;
                run = true;
            }
            else
            {
                return;
            }
        }

        if (run)
        {
            this.spoolThread = new Thread(new ThreadStart(spool));
            this.spoolThread.Start();
        }
    }

    private void spool()
    {
        Message.ITimingMessage message;

        while (this.messages.Count > 0)
        {
            // TODO: Is this below line necessary or does the TryDequeue cover this?
            message = null;

            this.messages.TryDequeue(out message);

            if (message != null)
            {
                // My long running thing that does something with this message.
            }
        }


        lock (lcurrentSpoolInstances)
        {
            this.currentSpoolInstances--;
        }
    }
  • 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-27T15:53:24+00:00Added an answer on May 27, 2026 at 3:53 pm

    This would be easier using BlockingCollection<T> instead of ConcurrentQueue<T>.

    Something like this should work:

    class MessageProcessor : IDisposable
    {
        BlockingCollection<IMyMessage> messages = new BlockingCollection<IMyMessage>();
    
        public MessageProcessor()
        {
           // Move this to constructor to prevent race condition in existing code (you could start multiple threads...
           Task.Factory.StartNew(this.spool, TaskCreationOptions.LongRunning);
        }
    
        public void AddMessage(IMyMessage message)
        {
            this.messages.Add(message);
        }
    
        private void Spool()
        {
             foreach(IMyMessage message in this.messages.GetConsumingEnumerable())
             {
                   // long running thing that does something with this message.
             }
        }
    
        public void FinishProcessing()
        {
             // This will tell the spooling you're done adding, so it shuts down
             this.messages.CompleteAdding();
        }
    
        void IDisposable.Dispose()
        {
            this.FinishProcessing();
        }
    }
    

    Edit: If you wanted to support multiple consumers, you could handle that via a separate constructor. I’d refactor this to:

        public MessageProcessor(int numberOfConsumers = 1)
        {
            for (int i=0;i<numberOfConsumers;++i)
                StartConsumer();
        }
    
        private void StartConsumer()
        {
           // Move this to constructor to prevent race condition in existing code (you could start multiple threads...
           Task.Factory.StartNew(this.spool, TaskCreationOptions.LongRunning);
        }
    

    This would allow you to start any number of consumers. Note that this breaks the rule of having it be strictly FIFO – the processing will potentially process “numberOfConsumer” elements in blocks with this change.

    Multiple producers are already supported. The above is thread safe, so any number of threads can call Add(message) in parallel, with no changes.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
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.