private void GoButton_Click(object sender, RoutedEventArgs e) { Hide(); Properties.Settings.Default.Port = _vm.Ports.SelectedItem; Properties.Settings.Default.FilterCOMPorts = _vm.FilterCOMPorts; Properties.Settings.Default.Save(); IControllerReader reader = InputSource.PRINTER.BuildReader("COM16"); try { _ = new GameBoyPrinterEmulatorWindow(reader).ShowDialog(); } catch (UnauthorizedAccessException ex) { _ = MessageBox.Show(ex.Message, _resources.GetString("RetroSpy", CultureInfo.CurrentUICulture), MessageBoxButton.OK, MessageBoxImage.Error); } Show(); }
private void UpdatePortList() { if (!isClosing && Monitor.TryEnter(updatePortLock)) { try { List <string> arduinoPorts = SetupCOMPortInformation(); foreach (string port in arduinoPorts) { using (SerialPort _serialPort = new SerialPort(port, 115200, Parity.None, 8, StopBits.One) { Handshake = Handshake.None, ReadTimeout = 500, WriteTimeout = 500 }) { try { _serialPort.Open(); } catch (Exception) { continue; } try { _serialPort.Write("\x88\x33\x0F\x00\x00\x00\x0F\x00\x00"); } catch (Exception) { _serialPort.Close(); continue; } try { string result = null; do { result = _serialPort.ReadLine(); } while (result != null && (result.StartsWith("!", StringComparison.Ordinal) || result.StartsWith("#", StringComparison.Ordinal))); if (result == "parse_state:0\r" || result.Contains("d=debug")) { _serialPort.Close(); Thread.Sleep(1000); if (Dispatcher.CheckAccess()) { Hide(); } else { Dispatcher.Invoke(() => { Hide(); }); } Properties.Settings.Default.Port = _vm.Ports.SelectedItem; Properties.Settings.Default.FilterCOMPorts = _vm.FilterCOMPorts; Properties.Settings.Default.Save(); try { if (Dispatcher.CheckAccess()) { IControllerReader reader = InputSource.PRINTER.BuildReader(port); _ = new GameBoyPrinterEmulatorWindow(reader).ShowDialog(); } else { Dispatcher.Invoke(() => { IControllerReader reader = InputSource.PRINTER.BuildReader(port); _ = new GameBoyPrinterEmulatorWindow(reader).ShowDialog(); }); } } catch (UnauthorizedAccessException ex) { _ = MessageBox.Show(ex.Message, _resources.GetString("RetroSpy", CultureInfo.CurrentUICulture), MessageBoxButton.OK, MessageBoxImage.Error); } if (Dispatcher.CheckAccess()) { Show(); } else { Dispatcher.Invoke(() => { Show(); }); } } else { _serialPort.Close(); continue; } } catch (Exception) { _serialPort.Close(); continue; } } } } catch (TaskCanceledException) { // Closing the window can cause this due to a race condition } finally { Monitor.Exit(updatePortLock); } } }