private void btnDisconnect_Click()

in Hands-on lab/lab-files/starter-project/SmartMeterSimulator/MainForm.cs [138:168]


        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            //Stop Loop
            stopQueue = true;
            
            foreach(Button button in pDevices.Controls)
            {
                //Only select devices which have been installed
                if (button.Tag == null)
                    continue;

                button.MouseHover -= Device_Hover;

                //Retrieve the sensor
                Sensor sensor = (Sensor)button.Tag;

                //Select devices that are connected and communicating
                if(sensor.State == DeviceState.Connected || sensor.State == DeviceState.Transmit)
                { 
                    //Disconnect the device, revert to Registered State
                    sensor.DisconnectDevice();

                    //Set color back to Cyan
                    button.BackColor = Color.Cyan;

                    //Set the status window off
                    if (!string.IsNullOrEmpty(sensor.StatusWindow))
                        pStatus.Controls[sensor.StatusWindow].Visible = false;
                }               
            }                      
        }