diff --git a/ArtNetSharp/ArtNet.cs b/ArtNetSharp/ArtNet.cs index 85ca66f..0a5b477 100644 --- a/ArtNetSharp/ArtNet.cs +++ b/ArtNetSharp/ArtNet.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using RDMSharp; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -30,8 +31,8 @@ public static ArtNet Instance private Dictionary ipTomacAddressCache = new Dictionary(); - private List instances = new List(); - public ReadOnlyCollection Instances { get => instances.AsReadOnly(); } + private ConcurrentDictionary instances = new ConcurrentDictionary(); + public ReadOnlyCollection Instances { get => instances.Values.ToList().AsReadOnly(); } private List networkClients = new List(); public IReadOnlyCollection NetworkClients => networkClients.AsReadOnly(); @@ -332,7 +333,7 @@ private void processPacket(AbstractArtPacketCore packet, IPv4Address localIp, IP Logger.LogTrace($"Received Packet from {sourceIp} -> {packet}"); //#endif - instances.ForEach(_instance => { ((IInstance)_instance).PacketReceived(packet, localIp, sourceIp); }); + instances.Values.ToList().ForEach(_instance => { ((IInstance)_instance).PacketReceived(packet, localIp, sourceIp); }); } public static bool IsNetworkAvailable(long? minimumSpeed=null) @@ -434,12 +435,17 @@ private void updateNetworkClients() public void AddInstance(AbstractInstance instance) { - this.instances.Add(instance); + if (this.instances.Any(i => i.Value == instance)) + return; + this.instances.TryAdd((uint)new Random().Next(), instance); Logger?.LogDebug($"Added instance {instance.GetType().Name}: {instance.Name} ({instance.ShortName})"); } public void RemoveInstance(AbstractInstance instance) { - this.instances.Remove(instance); + var toRemove = this.instances.FirstOrDefault(i => i.Value == instance); + if (toRemove.Value == null) + return; + this.instances.TryRemove(toRemove.Key, out _); Logger?.LogDebug($"Removed instance {instance.ShortName}"); }