private async void ButtonConnectToDevice_Click(object sender, RoutedEventArgs e) { var selection = COMcombobox.SelectedItem; DeviceListEntry entry = null; if (selection != null) { var obj = selection; entry = (DeviceListEntry)obj; if (entry != null) { // Create an EventHandlerForDevice to watch for the device we are connecting to EventHandlerForDevice.CreateNewEventHandlerForDevice(); // Get notified when the device was successfully connected to or about to be closed EventHandlerForDevice.Current.OnDeviceConnected = this.OnDeviceConnected; EventHandlerForDevice.Current.OnDeviceClose = this.OnDeviceClosing; // It is important that the FromIdAsync call is made on the UI thread because the consent prompt, when present, // can only be displayed on the UI thread. Since this method is invoked by the UI, we are already in the UI thread. Boolean openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector); // Disable connect button if we connected to the device UpdateConnectDisconnectButtonsAndList(!openSuccess); if (openSuccess) { comText.IsReadOnly = false; ResetReadCancellationTokenSource(); ResetWriteCancellationTokenSource(); IsReadTaskPending = true; DataReaderObject = new DataReader(EventHandlerForDevice.Current.Device.InputStream); await ReadAsync(ReadCancellationTokenSource.Token); } } } }