/// <summary> /// Method to dispose of current Lamp instance /// and release all resources /// </summary> private void DisposeLamp() { lampToggle.IsEnabled = false; lamp.AvailabilityChanged -= Lamp_AvailabilityChanged; lamp.IsEnabled = false; lamp.Dispose(); lamp = null; }
/// <summary> /// acquires Lamp instance by getting class selection string /// for Lamp devices, then selecting the first lamp device on /// the back of the device /// </summary> /// <param name="sender">Contains information regarding button that fired event</param> /// <param name="e">Contains state information and event data associated with the event</param> private async void InitLampBtn_Click(object sender, RoutedEventArgs e) { try { await LogStatusToOutputBoxAsync("Getting class selection string for lamp devices..."); // Returns class selection string for Lamp devices string selectorStr = Lamp.GetDeviceSelector(); await LogStatusToOutputBoxAsync(string.Format(CultureInfo.InvariantCulture, "Lamp class selection string:\n {0}", selectorStr)); await LogStatusToOutputBoxAsync("Finding all lamp devices..."); // Finds all devices based on lamp class selector string DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selectorStr); await LogStatusToOutputBoxAsync(string.Format(CultureInfo.InvariantCulture, "Number of lamp devices found: {0}", devices.Count.ToString())); // Select the first lamp device found on the back of the device await LogStatusToOutputBoxAsync("Selecting first lamp device found on back of the device"); DeviceInformation deviceInfo = devices.FirstOrDefault(di => di.EnclosureLocation != null && di.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back); if (deviceInfo == null) { throw new InvalidOperationException("Error: No lamp device was found"); } await LogStatusToOutputBoxAsync(string.Format(CultureInfo.InvariantCulture, "Acquiring Lamp instance from Id:\n {0}", deviceInfo.Id)); Lamp lamp = await Lamp.FromIdAsync(deviceInfo.Id); await LogStatusAsync(string.Format(CultureInfo.InvariantCulture, "Lamp instance acquired, Device Id:\n {0}", lamp.DeviceId), NotifyType.StatusMessage); // Here we must Dispose of the lamp object once we are no longer // using it to release any native resources await LogStatusToOutputBoxAsync("Disposing of lamp instance"); lamp.Dispose(); lamp = null; await LogStatusToOutputBoxAsync("Disposed"); } catch (Exception eX) { await LogStatusAsync(eX.Message, NotifyType.ErrorMessage); } }