public static void ReleaseLoader(CSerialPortLibraryLoader loader) { lock (loaderInstances) { loader.refCount--; if (loader.refCount == 0) { ((IDisposable)loader).Dispose(); loaderInstances.Remove(loader.dynamicLinkLibrariesPath.FullName); } } }
public static CSerialPortLibraryLoader GetOrCreateLoader(DirectoryInfo dynamicLinkLibrariesPath) { if (!dynamicLinkLibrariesPath.Exists) { throw new DirectoryNotFoundException(); } lock (loaderInstances) { if (loaderInstances.ContainsKey(dynamicLinkLibrariesPath.FullName)) { var instance = loaderInstances[dynamicLinkLibrariesPath.FullName]; instance.refCount++; return(instance); } var returnedInstance = new CSerialPortLibraryLoader(dynamicLinkLibrariesPath); loaderInstances[dynamicLinkLibrariesPath.FullName] = returnedInstance; return(returnedInstance); } }
public void Dispose() { instance.Dispose(); CSerialPortLibraryLoader.ReleaseLoader(this.libraryLoader); }
public CSerialPortManager(DirectoryInfo dynamicLinkLibrariesPath) { this.libraryLoader = CSerialPortLibraryLoader.GetOrCreateLoader(dynamicLinkLibrariesPath); instance = new CSerialPortInstance(this, libraryLoader.GetInteropDelegate <CreateNewInstance>().Invoke()); }