/// <summary> /// Prevents a default instance of the <see cref="DAC"/> class from being created. /// </summary> /// <param name="type">The type.</param> /// <param name="name">The name. Can be empty or null.</param> /// <param name="device">The device number of this <see cref="DAC"/>.</param> DAC(ControllerType type, string name, uint device) { if (instance != null) { throw new InvalidOperationException("DAC not initialized. Call DAC.TryInitialize(...) first."); } this.type = type; this.name = name; this.device = device; this.shutter = new ReferenceCounter(OnCloseShutter, OnOpenShutter); instance = this; }
/// <summary> /// Tries to initialize the <see cref="DAC.Instance"/> using the specified <see cref="ControllerType"/> mask. /// </summary> /// <param name="types">The types of controllers that should be used during initialization.</param> /// <param name="type">The type of controllers to use.</param> /// <param name="deviceChooser">The device chooser. Once the overall number of devices has been retrieved, this /// callback is asked which device should actually be used. Can be null. If null, the first device that has been /// found will be used.</param> /// <exception cref="T:System.InvalidOperationException">If <see cref="DAC.Instance"/> has been initialized.</exception> /// /// <exception cref="T:System.ArgumentException">If any error occurred while initializing the device.</exception> public static DAC Initialize(ControllerTypes types, ControllerType type, DeviceChooser deviceChooser = null) { if (instance != null) { throw new InvalidOperationException(string.Format("DAC already initialized with '{0}'. Shutdown/Dispose DAC before retrieving new information.", instance.ControllerType)); } try { uint deviceCount = 0; if (NativeMethods.LDL_Init((uint)ConvertToNativeMask(types), ref deviceCount) != NativeConstants.DAC_OK) { throw new ArgumentException(string.Format("Could not intialize DAC with '{0}'", type)); } uint device = deviceChooser == null ? 0 : deviceChooser(deviceCount); uint tType = 0; uint tEnum = 0; var sName = new StringBuilder(128); if (NativeMethods.LDL_GetDACInfo(device, sName, (uint)sName.Length, ref tType, ref tEnum) != NativeConstants.DAC_OK) { throw new ArgumentException(string.Format("Could retriev name of DAC device number {0} with '{1}'.", device, type)); } if (NativeMethods.LDL_DAC_Init(device) != NativeConstants.DAC_OK) { throw new ArgumentException(string.Format("Could not intialize DAC with '{0}' using device number {1}", type, device)); } instance = new DAC(type, sName.ToString(), device); return(instance); } catch (Exception ex) { NativeMethods.LDL_Close(); throw new ArgumentException(string.Format("Could not initialize DAC.Instance with '{0}'.", type), ex); } }
void Dispose(bool disposing) { try { if (!disposing || disposed) { return; } NativeMethods.LDL_DAC_Stop(device); NativeMethods.LDL_Close(); } finally { if (instance == this) { instance = null; } disposed = true; } }