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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:40:03+00:00 2026-05-28T13:40:03+00:00

I am developing an application simulating a network comprising a number of nodes that

  • 0

I am developing an application simulating a network comprising a number of nodes that exchange messages. I try to simulate the transmission channel with a Queue where every node can place a message. Then, another entity takes over the message and delivers it to the specified node. Then, i want to signal (with an event) the end of a transmission phase when the message queue is idle for a certain amount of time, say X, namely no new messages have been added to the queue for X mseconds.

I understand that my case follows the consumer/producer paradigm. So far, i have done the following:

public class Com<T>
{
    private Thread dispatcher;
    private Queue<T> queue;
    private int waitTime;
    private Object locker;
    private Timer timer;
    public event EventHandler EmptyQueueEvent;

    public Com()
    {
        queue = new Queue<T>();
        locker = new Object();
        waitTime = X;
        timer = new Timer(FireEmpty, null, Timeout.Infinite,Timeout.Infinite);
        dispatcher = new Thread(Serve);
        dispatcher.IsBackground = true;
                    dispatcher.Start();
    }

    private void Serve()
    {
        while (true)
        {
            try
            {
                if (queue.Count == 0)
                {
                    timer.Change(waitTime, 0);
                    Thread.Sleep(Timeout.Infinite);
                }
            }
            catch (ThreadInterruptedException)
            {
            }
            while (queue.Count != 0)
            {
                lock (locker)
                {
                    deliver(queue.Dequeue());
                }
            }
        }
    }

    private void deliver(T item)
    {
        // Do stuff
    }

    public void Add(T item)
    {
        timer.Change(Timeout.Infinite, Timeout.Infinite);
        lock (locker)
        {
            queue.Enqueue(item);
        }
        dispatcher.Interrupt();
    }

    private void FireEmpty(object o)
    {
        //Fire Event
    }
}

However, running my simulations proves that my synchronization is not enough, since I am sometimes getting a “ThreadInterruptedException” while trying to dequeue my message (in method Serve()). Note that the exception does not occur each time i run the simulation, but rather rarely: approximately every 850-1000 executions (i am running the execution iteratively)..

Does anybody have an idea what it is wrong with my code? 🙂

  • 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-28T13:40:04+00:00Added an answer on May 28, 2026 at 1:40 pm

    Have you tried locking before you attempt to get the Queue count? Like:

        private void Serve()
        {
            while (true)
            {
                try
                {
                    int count = 0;
                    lock(locker)
                       count= queue.Count;
                    if (count == 0)
                    {
                        timer.Change(waitTime, 0);
                        Thread.Sleep(Timeout.Infinite);
                    }
                }
                catch (ThreadInterruptedException)
                {
                }
                while (queue.Count != 0)
                {
                    lock (locker)
                    {
                        deliver(queue.Dequeue());
                    }
                }
            }
        }
    

    It’s possible that an add is getting called at the same time you’re trying to count the number of items. Also, you might want to consider one of the collections from System.Collections.Concurrent if you’re using .net 4.0.

    ** UPDATE **

    I just took a closer look at your code and had an “Oh duh” moment. You should be getting a ThreadInterruptException because you’re calling delegate.Interrupt(). Check the MSDN documentation on that. I think what you need to do is use something like a ManualResetEvent and instead of calling Interrupt() do a WaitOne() on that event.

    ** UPDATE2 **
    Here’s some sample code that includes my other locking suggestion as well (on Gist):
    https://gist.github.com/1683547

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

Sidebar

Related Questions

When developing an application that sends out notification email messages, what are the best
I'm developing application that will read messages from IMAP server.. what would you recommend
I'm developing application with GWT 2 and would like to add float panel that
i'm developing application that is listening to the data coming to the pc and
I am developing application that works with sql server, I read and update data
I am developing application in N-Tier Architecture. as we all know that we need
I'm developing application that would communicate with a mobile app. My problem is as
I'm developing application that require send shortcut to desktop operation. I found only one
I'm developing application that start page depends on file existing in isolated storage. If
I am developing application where i need to get users phone number. Now i

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.