/// <summary> /// This method demonstrates how to close the device properly using the WinRT Serial API. /// /// When the SerialDevice is closing, it will cancel all IO operations that are still pending (not complete). /// The close will not wait for any IO completion callbacks to be called, so the close call may complete before any of /// the IO completion callbacks are called. /// The pending IO operations will still call their respective completion callbacks with either a task /// cancelled error or the operation completed. /// </summary> private async void CloseCurrentlyConnectedDevice() { if (Device != null) { // Notify callback that we're about to close the device OnDeviceClose?.Invoke(this, DeviceInformation); ActionQueue.Dispose(); Connection.Dispose(); // This closes the handle to the device Device.Dispose(); ActionQueue = null; Connection = null; Device = null; // Save the deviceInformation.Id in case deviceInformation is set to null when closing the // device var deviceName = DeviceInformation.Properties["System.ItemNameDisplay"] as string; await MainPage.Current.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { MainPage.Current.NotifyUser(deviceName + " is closed", NotifyType.StatusMessage); }); } }
/// <summary> /// Closes the device /// </summary> /// <remarks>When the device is closing, it will cancel all IO operations that are still pending (not complete).</remarks> private void CloseCurrentlyConnectedDevice() { if (IsDeviceConnected) { // Notify callback that we're about to close the device OnDeviceClose?.Invoke(this, DeviceInformation); // This closes the handle to the device if device is disposable // TODO: comprendre pourquoi disposer les device cause un exception et décommenter ça : // if (_device is IDisposable) // (_device as IDisposable).Dispose(); _device = null; } }
/// <summary> /// This method demonstrates how to close the device properly using the WinRT Serial API. /// /// When the SerialDevice is closing, it will cancel all IO operations that are still pending (not complete). /// The close will not wait for any IO completion callbacks to be called, so the close call may complete before any of /// the IO completion callbacks are called. /// The pending IO operations will still call their respective completion callbacks with either a task /// cancelled error or the operation completed. /// </summary> private async void CloseCurrentlyConnectedDevice() { if (Device != null) { // Notify callback that we're about to close the device OnDeviceClose?.Invoke(this, DeviceInformation); // This closes the handle to the device Device.Dispose(); // Save the deviceInformation.Id in case deviceInformation is set to null when closing the device Device = null; string deviceId = DeviceInformation.Id; await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(() => { MainPage.Current.NotifyUser(deviceId + " is closed", NotifyType.StatusMessage); })); } }