/// <summary>
 /// Listener for the connector's CheckingPort event.
 /// </summary>
 /// <param name="portName">Name of the next port the connector will be checking.</param>
 private void Connector_NextPort(string portName)
 {
     Dispatcher.Invoke(new Action(() =>
     {
         _curPortConnectionStatus = new PortConnectionStatus(portName);
         _portConnectionStatusList.Add(_curPortConnectionStatus);
     }
                                  ));
 }
        /// <summary>
        /// Initiates the connection process.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnOk_Click(object sender, RoutedEventArgs e)
        {
            // Clear the port connection status list
            _portConnectionStatusList.Clear();
            _curPortConnectionStatus = null;

            // Get the correct Connector
            IConnector connector = _conFactory.GetConnector(SelectedConnector, ConstructConnectionSettings());

            // Hook into the connector's events
            connector.CheckingPort          += Connector_NextPort;
            connector.UpdateMessages        += Connector_UpdateMessage;
            connector.ConnectionComplete    += Connector_Complete;
            connector.ConnectionEstablished += Connector_ConnectionEstablished;
            connector.PortSuccess           += Connector_PortSuccess;

            // If this application is already connected on a SerialPort, close the connection
            if (ELM327Connection.Connection != null && ELM327Connection.Connection.IsOpen)
            {
                lblStatus.Content = "Closing existing ELM327 connection on port " + ELM327Connection.Connection.PortName + "...";
                log.Info("Closing existing ELM327 connection on port [" + ELM327Connection.Connection.PortName + "].");
                ELM327Connection.DestroyConnection();
            }

            // Log
            log.Info("Spawning thread for finding an Elm327 port and connecting.");

            // Disable Controls
            btnOk.IsEnabled       = false;
            cmbPortList.IsEnabled = false;
            btnCancel.Content     = "Cancel";

            // Start our Progress Bar
            pbProgressBar.IsIndeterminate = true;

            // Update the Status
            lblStatus.Content = "Connecting...";

            // Store a reference to the new IConnector
            _runningConnector = connector;

            // Spawn a thread
            _getSerialPortThread = new Thread(new ThreadStart(connector.GetSerialPort));
            _getSerialPortThread.SetApartmentState(ApartmentState.STA);
            _getSerialPortThread.Start();
        }
 /// <summary>
 /// Listener for the connector's CheckingPort event.
 /// </summary>
 /// <param name="portName">Name of the next port the connector will be checking.</param>
 private void Connector_NextPort(string portName)
 {
     Dispatcher.Invoke(new Action(() =>
         {
             _curPortConnectionStatus = new PortConnectionStatus(portName);
             _portConnectionStatusList.Add(_curPortConnectionStatus);
         }
     ));
 }
        /// <summary>
        /// Initiates the connection process.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnOk_Click(object sender, RoutedEventArgs e)
        {
            // Clear the port connection status list
            _portConnectionStatusList.Clear();
            _curPortConnectionStatus = null;

            // Get the correct Connector
            IConnector connector = _conFactory.GetConnector(SelectedConnector, ConstructConnectionSettings());
            
            // Hook into the connector's events
            connector.CheckingPort += Connector_NextPort;
            connector.UpdateMessages += Connector_UpdateMessage;
            connector.ConnectionComplete += Connector_Complete;
            connector.ConnectionEstablished += Connector_ConnectionEstablished;
            connector.PortSuccess += Connector_PortSuccess;

            // If this application is already connected on a SerialPort, close the connection
            if (ELM327Connection.Connection != null && ELM327Connection.Connection.IsOpen)
            {
                lblStatus.Content = "Closing existing ELM327 connection on port " + ELM327Connection.Connection.PortName + "...";
                log.Info("Closing existing ELM327 connection on port [" + ELM327Connection.Connection.PortName + "].");
                ELM327Connection.DestroyConnection();
            }

            // Log
            log.Info("Spawning thread for finding an Elm327 port and connecting.");

            // Disable Controls
            btnOk.IsEnabled = false;
            cmbPortList.IsEnabled = false;
            btnCancel.Content = "Cancel";

            // Start our Progress Bar
            pbProgressBar.IsIndeterminate = true;

            // Update the Status
            lblStatus.Content = "Connecting...";

            // Store a reference to the new IConnector
            _runningConnector = connector;

            // Spawn a thread
            _getSerialPortThread = new Thread(new ThreadStart(connector.GetSerialPort));
            _getSerialPortThread.SetApartmentState(ApartmentState.STA);
            _getSerialPortThread.Start();
        }