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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:41:41+00:00 2026-06-02T21:41:41+00:00

So I have data that is processed in one thread and put into a

  • 0

So I have data that is processed in one thread and put into a queue, then another thread is dequeing the information and performing some actions on it.

Here is the queue

public static class MyConcurrentQueue
{
    public static ConcurrentQueue<cRule> _Queue;

    public static void EnqueueRuleTrigger(cRule _Rule)
    {
        MyConcurrentQueue._Queue.Enqueue(_Rule);
    }
}

on application_start i have

 MyConcurrentQueue._Queue = new ConcurrentQueue<cRule>();

if i want to enqueue a rule i use

MyConcurrentQueue.EnqueueRuleTrigger(_Rule); //We're done enqueue the rule

to dequeue I use

 MyConcurrentQueue._Queue.TryDequeue(out _Rule)

I noticed that if enqueued 2 items, 1 after the other in a tight loop that by the time of the first dequeue [separate asynch thread], the data in the log file didn’t match the enqueue anymore

If I put a sleep between the enqueues the log reads the correct values, probably because one item is put in, dequeued, another item put in, dequeued.

I suppose this is because the queue is static and it is storing a reference to _Rule instead of a copy of it. Any ideas on how I can fix this? I need the queue to be accessible to multiple threads that’s why I made it static in the first place.

Thanks in advance.

EDIT

Here is my enqueue and log and dequeue and log logic

while (x<=y)
                    {

  MyConcurrentQueue.EnqueueRuleTrigger(_Rule); //We're done enqueue the string AllText = File.ReadAllText(@"C:\Default\New.txt");
File.WriteAllText(@"C:\Default\New.txt", AllText + "\r\nEnqueue Alert:" +
 _Rule.AlertID +":" + _Rule.TriggerStartTime + "-" + _Rule.TriggerEndTime);

}

This is on a separate thread

if ( MyConcurrentQueue._Queue.TryDequeue(out _Rule))
                {
... some logic
     File.WriteAllText(@"C:\Default\New.txt", AllText + "\r\nDequeue Alert:" + 
_Rule.AlertID + ":" + _Rule.TriggerStartTime + "-" + _Rule.TriggerEndTime);
    }

this is my log file

Enqueue Alert:64c88289-58a1-499b-ade9-3fa69a32cf47: 4/27/2012 12:00:00 PM-4/27/2012 5:00:00 PM

Enqueue Alert:64c88289-58a1-499b-ade9-3fa69a32cf47: 4/28/2012 2:00:00 PM-4/28/2012 9:00:00 PM

Dequeue Alert:64c88289-58a1-499b-ade9-3fa69a32cf47: 4/28/2012 2:00:00 PM-4/28/2012 9:00:00 PM

Dequeue Alert:64c88289-58a1-499b-ade9-3fa69a32cf47: 4/28/2012 2:00:00 PM-4/28/2012 9:00:00 PM

  • 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-06-02T21:41:42+00:00Added an answer on June 2, 2026 at 9:41 pm

    EDIT: Seems to be an issue with enqueing an object, than changing the same object and enqueing it second time. As result during dequeue it looks like second object overrode the first and was inserted twice, in reality reference to the same object was inserted twice.

    Most likely solution is to deep clone before insert. Consider making your objects immutable to simplify synchronization and readability of the code.

    Original:

    Most likly reason is your logging code is not properly synchronized. Since it looks like you want 2 action to happen atomically (Enqueue + Log or Dequeue+Log) you have to add proper locking around both operations, otherwise order of calls to Queue and Log may be semi-random. Also make sure you correctly handle result of TryDequeue (since it can return false).

    static object logAndQueueLock = new object();
    public static void EnqueueRuleTrigger(Rule rule) 
    { 
        lock(logAndQueueLock)
        {
          MyConcurrentQueue._Queue.Enqueue(rule); 
          Log.Message("Enqueued:"+ rule.ToString());
        } 
    } 
    
    public static Rule DequeueRuleTrigger() 
    { 
        lock(logAndQueueLock)
        {
          Rule rule = null;
          if (MyConcurrentQueue._Queue.Enqueue(out rule)){ 
             Log.Message("Enqueued:"+ rule.ToString());
          }
          return rule; 
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data-upload function that load some data into several tables and processes
I have one worker role that throws data into around 10 queues that need
Suppose I have a class that processes some data: class SomeClass { public: void
I have a C#/.Net job that imports data from Excel and then processes it.
I have some data generated in MATLAB that I want to process using Perl.
I have console application. In that i have some process that fetch the data
Background Information I have a distributed processing application that does data analysis. It is
I have a GUI application that process some data, it converts line of text
I have a 'producer' object, that steps through some data and emits various signals
Assume I have a application that stores data,gets data and processes data and stores

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.