Skip to content

Instantly share code, notes, and snippets.

@TomW-Skyline
Last active October 20, 2023 11:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save TomW-Skyline/805c689e62f0362d845ced2829a91aa1 to your computer and use it in GitHub Desktop.
namespace QAction_1
{
using System;
using System.Collections.Concurrent;
using Skyline.DataMiner.Scripting;
public class ElementInstanceManager
{
private static readonly ConcurrentDictionary<string, ElementInstance> Instances = new ConcurrentDictionary<string, ElementInstance>();
public static ElementInstance GetInstance(SLProtocol protocol)
{
string key = GetKey(protocol);
return Instances.GetOrAdd(key, k => new ElementInstance(k));
}
public static ElementInstance CreateNewInstance(SLProtocol protocol)
{
string key = GetKey(protocol);
var instance = new ElementInstance(key);
Instances[key] = instance;
return instance;
}
public static void RemoveInstance(SLProtocol protocol)
{
string key = GetKey(protocol);
Instances.TryRemove(key, out _);
}
private static string GetKey(SLProtocol protocol)
{
if (protocol == null)
throw new ArgumentNullException(nameof(protocol));
return String.Join("/", protocol.DataMinerID, protocol.ElementID);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment