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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:00:31+00:00 2026-05-31T19:00:31+00:00

I found the article regarding what I want to implement at Queue implementation in

  • 0

I found the article regarding what I want to implement at Queue implementation in C#. But the article is based on .Net 4.0. and MSDN website has very simple example only. so decided to post this here.

That is, I’d like to implement that a thread displays a string asynchronously in dos prompt window by taking the string from a queue. Another thread might add additional strings in the string queue asynchronously as well(string maximum entry is 20, once fill maximum entry, no more addition). And I’d like to run this application until the queue is empty based on .Net 2.0 or 3.5.(VS2008)

Queuing implementation looks simple because .Net provides it, but I don’t know how to implement asynchronous part with this queue.

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-31T19:00:32+00:00Added an answer on May 31, 2026 at 7:00 pm

    So what I gather is you want to use essentially the ConcurrentQueue but you’re stuck in .NET 3.5 and cannot use it (because it doesn’t exist).

    This goes back to simple operation locking. You don’t want two threads accessing the same object at the same time.

    Before You Start: Read This book on C# Threading by Joseph Albahari

    It goes over .NET Threading and even just skimming it will help you prevent running into bad threading mistakes.

    There’s 2 things you essentially need to do, so let’s break this down:

    Accessing the Queue safely

    You can use the Generic Queue<T> Class to implement your message queue, however all operations that change this object need to be handled safely.

    The simplest form of doing this is using a Lock Statement.

    lock(myQueue)
    {
       myQueue.Enqueue(message);
    }
    

    You must lock around all operations, so the three you should need are .Enqueue(...), .Dequeue() and .Count since they all access data.

    This will prevent you from running into multi-threading problems with queueing/dequeuing your messages and life will be good.

    Waiting for a Message

    To wait for a message you have a number of ways you can solve this. Most of them are outlined in the e-book i linked above.

    The simplest is a Thread.Sleep Loop

    while(appIsRunning)
    {
        Thread.Sleep(100);
        lock(myQueue)
        {
            while(myQueue.Count > 0)
            {
               Console.WriteLine(myQueue.Dequeue());
            }
        }
    }
    

    Note: This is not the BEST way to do it, just the simplest example

    What this does is simply schedules this thread to run again at least 100ms from now. (There’s no gaurantee it’ll run 100ms from now, just not before that time). Then it locks the queue so no writes can happen to it, it empties the queue and writes the lines to the screen then loops again.

    If the queue is empty it’ll just go back to sleeping.

    The other solution is using a Pulse/Wait

    Monitor.Pulse and Monitor.Wait are a ping-pong style thread control paradigm.

    You can have your loop in the main thread Monitor.Wait() and when you add a message to the queue you can Monitor.Pulse() to release the lock it’s to a waiting thread.

    This is more semantically relevant but probably less efficient because your context switch is happening on demand for each message.

    There’s about 10 other ways to do this, most are outlined in that book but that’s essentially the jist of it.

    Please see Event Signaling Section of Joseph’s Book for examples of all the signaling methods.

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

Sidebar

Related Questions

I've found an article on this subject by a Microsoft employee, but has anyone
I found this article regarding Linq-to-SQL and SQL Server connection pooling: MSDN Blog From
I found this article on how to manipulate the rendering sequence of asp.net controls.:
I've found this MSDN article that explains how to monitor processes and services with
I was reading this article about ADO.NET Entity Framework and found it to be
I found cool article on Creating cross platform GUI's with IronRuby where someone re-created
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
some time ago I found an article ( Roles: Composable Units of Object Behavior
I started by googling and found the article How to write INSERT if NOT
I found the following article : here about installing fonts on a Windows computer

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.