예제 #1
0
        /// <summary>
        /// Update serialComCmBx items, and keep existing
        /// ports at their current index in the list
        /// </summary>
        private void UpdateSerialPorts()
        {
            var t = Task.Run(delegate
            {
                // the following function is a blocking one, therefore must be
                // called in a seperate thread so GUI won't freeze
                connectedSerialPorts = SerialCom.GetConnectedPortsDetailedStrings();
            });

            if (connectedSerialPorts == null)
            {
                return;
            }

            // create a clone of serialComCmBx.Items to allow
            // modifying serialComCmBx.Items while iterating
            // over its elements
            List <string> portsListClone = new List <string>();

            foreach (string port in serialComCmBx.Items)
            {
                portsListClone.Add(port);
            }

            //remove disconnected ports from combobox
            foreach (string port in portsListClone)
            {
                if (!connectedSerialPorts.Contains(port))
                {
                    serialComCmBx.Items.Remove(port);
                }
            }

            // add new connected ports the combobox
            foreach (string port in connectedSerialPorts)
            {
                if (!serialComCmBx.Items.Contains(port))
                {
                    serialComCmBx.Items.Add(port);
                }
            }

            if (serialComCmBx.Items.Count > 0)
            {
                serialComCmBx.SelectedIndex = 0;
            }
        }