Search code examples
c#producer-consumer

What type of queue to use?


I have a static class called Monitor that includes a method LogToMonitor.

public static void LogToMonitor(MonitorDevice device, MonitorCategory category, MonitorType type, string msg)

It creates a MonitorEntry type and updates a property which does a property change to the UI and adds to an ObservableCollection.

 public ObservableCollection<MonitorEntry> MonitorEntries { get; }

I now have another thread that needs to LogToMonitor. This is going to cause issues having multiple calling threads. I'm thinking I should have a Producer Consumer approach.

What type of collection should I use?

Should the Queue be a separate class which processes the calls and updates the UI?

Can I have multiple threads still call LogToMonitor method which puts them in the Queue?


Solution

  • BlockingCollection<T> is what you are looking for probably. It's an implementation of the Producer-Consumer pattern. https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/blockingcollection-overview