I need to create an object that will represent the state of the table, and I want to be able to use that same object from other QActions, however my object needs to be declared on an element level, meaning each element of that protocol will have it's own single instance(singleton). I cannot use static as that would mean that all elements running that protocol will share the instance, and their table states may differ. I would like to avoid having to store the state in a dummy parameter within the actual table because that would beat the purpose of storing the state of the table in the object in the first place. Would this even be possible? I'm open to creative solutions as well, even if just as a though experiment.
Hi Edib, this can be achieved by using a static dictionary, with the element ID as key.
I once created the following class to make this more generic and easy to use: https://gist.github.com/TomW-Skyline/805c689e62f0362d845ced2829a91aa1.
Usage:
var instance = ElementInstanceManager.GetInstance(protocol);
var x = instance.MyData;
However do note that the data will remain in memory, even after restarting the element. To make sure that all data gets cleaned up when the element is being stopped (or deleted), it's important to call RemoveInstance in the IDisposable.Dispose() method of a QAction:
ElementInstanceManager.RemoveInstance(protocol);
Please let me know if more details are required.
Small note on the IDisposable.Dispose method, is only available from 10.2.9 onwards
See https://docs.dataminer.services/develop/devguide/Connector/LogicQActions.html#implementing-the-idisposable-interface
Prior to that version, an option could be to subscribe to the element state in order to detect when it gets stopped or deleted.