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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:33:38+00:00 2026-05-28T05:33:38+00:00

I’m inexperienced with threading and asynchronous processing so I’m not sure if what I’m

  • 0

I’m inexperienced with threading and asynchronous processing so I’m not sure if what I’m doing is safe.

UPDATE: This is restricted to .Net 3.5 unfortunately so no ConcurrentQueue (unless someone’s implemented it for .Net 3.5 or knows an alternative implementation?)

UPDATE 2: I found this approach using the source from a Mono implementation of ConcurrentQueue (which is perfectly compatible code for .Net 3.5).

I have one object who’s job it is to manage a Queue<MyObject> internally. This class needs to do its operations asynchronously (it will be running continuously between the OnStart and OnStop events of a Windows Service). Anyway, I’ll let the code do the talking:

public class MyObjectQueueManager : IMyObjectQueueManager
{
    private bool shouldContinueEnqueuing;
    private readonly Queue<MyObject> queue;
    private readonly IMyObjectIdentifier myObjectIdentifier;

    public MyObjectQueueManager( IMyObjectIdentifier myObjectIdentifier )
    {
        this.myObjectIdentifier = myObjectIdentifier;
        queue = new Queue<MyObject>();
    }

    public void StartQueue()
    {
        shouldContinueEnqueuing = true;

        var enqueuer = new MyObjectEnqueuer( EnqueueMyObjects );
        enqueuer.BeginInvoke( myObjectIdentifier, queue, StopEnqueuingMyObjects, new object() );

        var dequeuer = new MyObjectDequeuer( DequeueMyObjects );
        dequeuer.BeginInvoke( queue, StopDequeuingMyObjects, new object() );
    }

    public void StopQueue()
    {
        shouldContinueEnqueuing = false;
    }

    public event EventHandler<NextMyObjectEventArgs> OnNextMyObjectAvailable;

    private void EnqueueMyObjects( IMyObjectIdentifier identifier, Queue<MyObject> queue )
    {
        while ( shouldContinueEnqueuing )
        {
            var myObjects = identifier.GetMyObjects();

            foreach ( var myObject in myObjects )
            {
                queue.Enqueue( myObject );
            }

            WaitForQueueToShrink( queue, 1000 /* arbitrary queue limiter - will come from settings. */ );
        }
    }

    private void DequeueMyObjects( Queue<MyObject> queue )
    {
        while ( queue.Count > 0 || shouldContinueEnqueuing )
        {
            if ( queue.Count > 0 )
            {
                OnNextMyObjectAvailable( this, new NextMyObjectEventArgs( queue.Dequeue() ) );
            }

            Thread.Sleep( 1 );
        }
    }

    private static void WaitForQueueToShrink( ICollection queue, int queueLimit )
    {
        while ( queue.Count > queueLimit )
        {
            Thread.Sleep( 10 );
        }
    }

    private static void StopEnqueuingMyObjects( IAsyncResult result )
    {
        var asyncResult = ( AsyncResult ) result;
        var x = ( MyObjectEnqueuer ) asyncResult.AsyncDelegate;

        x.EndInvoke( asyncResult );
    }

    private static void StopDequeuingMyObjects( IAsyncResult result )
    {
        var asyncResult = ( AsyncResult ) result;
        var x = ( MyObjectDequeuer ) asyncResult.AsyncDelegate;

        x.EndInvoke( asyncResult );
    }

    private delegate void MyObjectEnqueuer( IMyObjectIdentifier myObjectIdentifier, Queue<MyObject> queue );

    private delegate void MyObjectDequeuer( Queue<MyObject> queue );
}

My notes/thoughts/concerns:

  • If I understand delegate.BeginInvoke correctly, I’m spawning two threads and both are accessing the Queue (through the invocation methods parameters).
  • One thread is only enqueuing items.
  • One thread is only dequeuing items, and it always checks that items are in the queue first.
  • I’m unsure if this is thread safe – will the queue get locked by one thread and not be accessible from the other thread?
  • If so, what alternative approach/technique could I use which would allow me to independently enqueue/dequeue items without holding up the main worker thread?
  • If this approach is actually somehow sound (you never know) will it scale to handle large volume? I.e. if the IMyObjectIdentifier is returning thousands of items per second will the scenario work (of course, I will be doing volume testing to confirm).

As a bonus, I’ve noticed that there is similar code being used to call EndInvoke from the StopEnqueuingMyObjects and StopDequeuingMyObjects methods – I’d like to refactor to use a single method if I can but when I tried using a generic method like this:

private static void StopProcessingMyObjects<T>( IAsyncResult result )
{
    var asyncResult = ( AsyncResult ) result;
    var x = ( T ) asyncResult.AsyncDelegate;

    x.EndInvoke( asyncResult ); 
}

It doesn’t like this because it doesn’t know that T is a delgate and so EndInvoke can’t be called. Is it possible to do this?

  • 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-28T05:33:39+00:00Added an answer on May 28, 2026 at 5:33 am

    Try the ConcurrentQueue<T>. This is a thread safe implementation of a Queue. The API is slightly different so it might require some modification of current code to implement.

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

Sidebar

Related Questions

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
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have some data like this: 1 2 3 4 5 9 2 6

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.