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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:56:24+00:00 2026-05-21T19:56:24+00:00

I have exactly 100 Sensors each measuring own data. I have exactly one DataSender

  • 0

I have exactly 100 Sensors each “measuring” own data. I have exactly one DataSender which should send information from “sensors”. The most recent information should be sent.

Bandwidth of the channel may be less than data produced by 100 sensors. In this case some data can be skipped – but we should be “roughly fair”. For example, we could skip every second measurement from each sensor.

I don’t know how often each sensor generates data, but in general they generate data pretty often.

After my other posts:

  • how to create singleton which always running in separate thread?
  • Modified Producer/Consumer example, any problems with it?

I have decided that I have classical Producer/Consumer problem, with:

  • 100 Producers, and
  • 1 Consumer

I’ve been suggested to use BlockingCollection for this. The only problem with BlockingCollection – once you have added item, you cannot replace it. But in my application, if sensor produces a new value, and previous value was not processed by the Consumer, the value should be replaced.

Should I use use a ConcurentDictionary or ConcurentBag for that task?

Conceptually, all I need is an array of 100 elements.

Sensor #33 should replace it’s value into array[33]:

| Sensor | Value |
|--------|-------|
|      1 |       |
|      2 |       |
|      3 |       |
/......../......./
|     32 |       |
|     33 | 101.9 |
|     34 |       |
/......../......./
|     98 |       |
|     99 |       |
|    100 |       |

Consumer should take value from array[33] and if not null, then send it and set array[33] to null. Consumer should react on any not null values in array asap.

  • 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-21T19:56:24+00:00Added an answer on May 21, 2026 at 7:56 pm

    I think you should implement your own IProducerConsumerCollection<T>. That’s why it’s an interface: so that you could easily make your own.

    You could do it using Dictionary<K,V> and Queue<T> to make sure receiving the data is fair, i.e. if you have just one device that produces data very fast, you won’t send data just from this one.

    public class DeviceDataQueue<TDevice, TData>
        : IProducerConsumerCollection<Tuple<TDevice, TData>>
    {
        private readonly object m_lockObject = new object();
        private readonly Dictionary<TDevice, TData> m_data
            = new Dictionary<TDevice, TData>();
        private readonly Queue<TDevice> m_queue = new Queue<TDevice>();
    
        //some obviously implemented methods elided, just make sure they are thread-safe
    
        public int Count { get { return m_queue.Count; } }
    
        public object SyncRoot { get { return m_lockObject; } }
    
        public bool IsSynchronized { get { return true; } }
    
        public bool TryAdd(Tuple<TDevice, TData> item)
        {
            var device = item.Item1;
            var data = item.Item2;
    
            lock (m_lockObject)
            {
                if (!m_data.ContainsKey(device))
                    m_queue.Enqueue(device);
    
                m_data[device] = data;
            }
    
            return true;
        }
    
        public bool TryTake(out Tuple<TDevice, TData> item)
        {
            lock (m_lockObject)
            {
                if (m_queue.Count == 0)
                {
                    item = null;
                    return false;
                }
    
                var device = m_queue.Dequeue();
                var data = m_data[device];
                m_data.Remove(device);
                item = Tuple.Create(device, data);
                return true;
            }
        }
    }
    

    When used along these lines:

    Queue = new BlockingCollection<Tuple<IDevice, Data>>(
        new DeviceDataQueue<IDevice, Data>());
    
    Device1 = new Device(1, TimeSpan.FromSeconds(3), Queue);
    Device2 = new Device(2, TimeSpan.FromSeconds(5), Queue);
    
    while (true)
    {
        var tuple = Queue.Take();
        var device = tuple.Item1;
        var data = tuple.Item2;
    
        Console.WriteLine("{0}: Device {1} produced data at {2}.",
            DateTime.Now, device.Id, data.Created);
    
        Thread.Sleep(TimeSpan.FromSeconds(2));
    }
    

    it produces the following output:

    30.4.2011 20:40:43: Device 1 produced data at 30.4.2011 20:40:43.
    30.4.2011 20:40:45: Device 2 produced data at 30.4.2011 20:40:44.
    30.4.2011 20:40:47: Device 1 produced data at 30.4.2011 20:40:47.
    30.4.2011 20:40:49: Device 2 produced data at 30.4.2011 20:40:49.
    30.4.2011 20:40:51: Device 1 produced data at 30.4.2011 20:40:51.
    30.4.2011 20:40:54: Device 2 produced data at 30.4.2011 20:40:54.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have about 100 stylesheets that apply the same style to one document, exactly
what does kill exactly do? I have a parent process which is creating 100
I have two LINQ objects which have exactly the same columns and I would
If I have an array of doubles that each have EXACTLY two decimal places,
I have a survey from 100 users and I'm trying to calculate some statistics.
I have 100 models (matrices), where each matrix's size is 4X3. Each of the
Consider that I have 100 instances of a squeaker class. Each squeaker object is
I have got a price field to display which sometimes can be either 100
I have a remote_form which works 100% When a user clicks submit, it goes
So I have 2 matrices in MATLAB. If one of them is a 100

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.