'Using C# SharpADBClient to output device info to textbox duplicates text on device connection

So I'm using SharpADBClient library to communicate to an android device via C# application. I'm able to query the data from the device and the first time the device is connected it displays the correct data. I'm also open to any refactoring tips as I'm a novice. Thank You.

Outputted Data to textbox(statusHome) 1st Connection

Device Name: Fire TV Stick 2
Fire Stick TV 2 is connected to this PC
ADB Version: 32
ADB Running Version: Version 1.0.32 of the adb daemon is running.
ADB Server Status: The adb daemon is not running.
ADB Default Server Port: 5037
ADB Default Port: 5555
ADB Default Encoding: ISO-8859-1
Device Serial: G070L81782241RAK
Device State: Online
Device Model: AFTT
Product Name: full_tank
Product Features: 
Device USB Port: 
Transport ID: 
Device Message: 
Connection Endpoint: 127.0.0.1:5555
Connection Serialization: InterNetwork:16:{21,179,127,0,0,1,0,0,0,0,0,0,0,0}
Connection Type: System.Net.IPEndPoint
Connection Serialization: 4

If I disconnect the device and reconnect it, it displays duplicated textbox(statusHome):

Device Name: Fire TV Stick 2
Device Name: Fire TV Stick 2
Fire Stick TV 2 is connected to this PC
Fire Stick TV 2 is connected to this PC
ADB Version: 32
ADB Version: 32
ADB Running Version: Version 1.0.32 of the adb daemon is running.
ADB Running Version: Version 1.0.32 of the adb daemon is running.
ADB Server Status: The adb daemon is not running.
ADB Server Status: The adb daemon is not running.
ADB Default Server Port: 5037
ADB Default Server Port: 5037
ADB Default Port: 5555
ADB Default Port: 5555
ADB Default Encoding: ISO-8859-1
ADB Default Encoding: ISO-8859-1
Device Serial: G070L81782241RAK
Device Serial: G070L81782241RAK
Device State: Online
Device State: Online
Device Model: AFTT
Device Model: AFTT
Product Name: full_tank
Product Name: full_tank
Product Features: 
Product Features: 
Device USB Port: 
Device USB Port: 
Transport ID: 
Transport ID: 
Device Message: 
Device Message: 
Connection Endpoint: 127.0.0.1:5555
Connection Endpoint: 127.0.0.1:5555
Connection Serialization: InterNetwork:16:{21,179,127,0,0,1,0,0,0,0,0,0,0,0}
Connection Serialization: InterNetwork:16:{21,179,127,0,0,1,0,0,0,0,0,0,0,0}
Connection Type: System.Net.IPEndPoint
Connection Type: System.Net.IPEndPoint
Connection Serialization: 4
Connection Serialization: 4

If I disconnect it a 3rd or 4th time it will just duplicate the text that many times.

Here is my code:

using System;
using System.Windows.Forms;
using SharpAdbClient;
using System.Net;
using System.Linq;

namespace Firemax
{
    public partial class indexControl : UserControl
    {
        readonly string newLine = Environment.NewLine;
        private string deviceData1;
        private string deviceData2;
        private string deviceData3;
        private string deviceData4;
        private string deviceData5;
        private string deviceData6;
        private string deviceData7;
        private string deviceData8;
        private string deviceData9;
        private string deviceData10;
        private string deviceData11;
        private string deviceData12;
        private string deviceData13;
        private string deviceData14;
        private string deviceData15;
        private string deviceData16;
        private string deviceData17;
        private string deviceData18;
        private string deviceData19;
        private string deviceData20;
        private string deviceData21;
        private string deviceData22;
        private string deviceData23;
        private string deviceData24;
        private string deviceData25;


        AdbServer adbServer = new AdbServer();
        AdbServerStatus adbStatus = new AdbServerStatus();

        public indexControl()
        {
            InitializeComponent();
            var monitor = new DeviceMonitor(new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)));
            monitor.DeviceConnected += this.OnDeviceConnected;
            monitor.DeviceDisconnected += this.OnDeviceDisConnected;
            monitor.Start();

        }
        public delegate void SetTextCallback(string text);
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.statusHome.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                statusHome.Text += text;
            }
        }

        void OnDeviceConnected(object sender, DeviceDataEventArgs e)
        {
            var result = adbServer.StartServer(@"C:\adb\adb.exe", restartServerIfNewer: true);
            var devices = AdbClient.Instance.GetDevices();
            int adbVersion = AdbClient.Instance.GetAdbVersion();

            foreach (var device in devices)
            {
                var displayFS2 = "Fire Stick TV 2";
                if (device.Model == "AFTT")
                {

                    deviceData1 += displayFS2 + " is connected to this PC" + newLine;
                    deviceData2 += "ADB Version: " + adbVersion + newLine;
                    deviceData4 += "ADB Running Version: " + adbServer.GetStatus() + newLine;
                    deviceData5 += "ADB Server Status: " + adbStatus.ToString() + newLine;
                    deviceData6 += "ADB Default Server Port: " + AdbClient.AdbServerPort + newLine;
                    deviceData7 += "ADB Default Port: " + AdbClient.DefaultPort + newLine;
                    deviceData8 += "ADB Default Encoding: " + AdbClient.DefaultEncoding + newLine;
                    deviceData9 += "Device Serial: " + device.Serial + newLine;
                    deviceData10 += "Device State: " + device.State + newLine;
                    deviceData11 += "Device Model: " + device.Model + newLine;
                    deviceData12 += "Product Name: " + device.Product + newLine;

                    if (device.Model == "AFTT")
                    {
                        deviceData13 += "Device Name: " + "Fire TV Stick 2" + newLine;
                        SetText(deviceData13.ToString());
                    }
                    else
                    {
                        deviceData14 += "Device Name: " + "Unknown Model" + newLine;
                        SetText(deviceData14.ToString());
                    }

                    deviceData15 += "Product Features: " + device.Features + newLine;
                    deviceData16 += "Device USB Port: " + device.Usb + newLine;
                    deviceData17 += "Transport ID: " + device.TransportId + newLine;
                    deviceData18 += "Device Message: " + device.Message + newLine;
                    deviceData19 += "Connection Endpoint: " + AdbClient.DefaultEndPoint.ToString() + newLine;
                    deviceData20 += "Connection Serialization: " + AdbClient.DefaultEndPoint.Serialize() + newLine;
                    deviceData21 += "Connection Type: " + AdbClient.DefaultEndPoint.GetType() + newLine;
                    deviceData22 += "Connection Serialization: " + 4 + newLine;

                    SetText(deviceData1.ToString());
                    SetText(deviceData2.ToString());
                    SetText(deviceData4.ToString());
                    SetText(deviceData5.ToString());
                    SetText(deviceData6.ToString());
                    SetText(deviceData7.ToString());
                    SetText(deviceData8.ToString());
                    SetText(deviceData9.ToString());
                    SetText(deviceData10.ToString());
                    SetText(deviceData11.ToString());
                    SetText(deviceData12.ToString());
                    SetText(deviceData15.ToString());
                    SetText(deviceData16.ToString());
                    SetText(deviceData17.ToString());
                    SetText(deviceData18.ToString());
                    SetText(deviceData19.ToString());
                    SetText(deviceData20.ToString());
                    SetText(deviceData21.ToString());
                    SetText(deviceData22.ToString());
                }
                else
                {
                    deviceData23 = "An unknown Android device is connected to this PC" + newLine;
                    SetText(deviceData23.ToString());
                }
            }
            Console.WriteLine($"The device {e.Device.Name} has connected to this PC");
        }
        void OnDeviceDisConnected(object sender, DeviceDataEventArgs e)
        {
            deviceData25 = "There is no device connected to this PC" + newLine;
            SetText(deviceData25.ToString());
        }
        private void bunifuMetroTextbox1_OnValueChanged(object sender, EventArgs e)
        {

        }
        private void statusHome_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


Solution 1:[1]

Cause of the problem

When assigning the deviceData variables, you used += which adds what you're assigning to what is already stored in the variable.

So when you connect the device to the computer, it triggers the OnDeviceConnected function, which adds "Fire Stick TV 2 is connected to this PC" to the deviceData1 variable. Then when you connect the device again, it triggers the OnDeviceConnected function again, and adds "Fire Stick TV 2 is connected to this PC" to the variable that already contains "Fire Stick TV 2 is connected to this PC".

Simple solution

You could simply assign each deviceData variable using just = so that it overrides whatever text was previously stored in that variable. The problem with that is that your program will no longer support connecting more than one device, because it'll keep on overriding what was stored in the variable.

Better solution

The solution to that would be to have a Device class with all the various properties that you'd like to track, and then instantiate a new Device object each time a new device is plugged in to the computer.

To make sure that you're not duplicating devices if you remove a device and plug it in again, you could have a list of your Device objects, then when detecting a new device connected, only instantiate it if the serial number is not in the list of other devices.

    List<Device> MyDeviceList = new List<Device>();

    void OnDeviceConnected(object sender, DeviceDataEventArgs e)
    {
        string newDeviceSerial = e.Device.Serial;
        bool wasAlreadyConnected = false;
        foreach(var device in MyDeviceList)
        {
            if(device.Serial == newDeviceSerial)
            {
                wasAlreadyConnected = true;
            }
        }
        if(!wasAlreadyConnected)
        {
             // Instantiate a new Device object, with all the details from the newly connected device
             // Add the new Device object to MyDeviceList
        }
    }

Hope that helps ;)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Naftoli Ost