Duplicates generated when running threaded string builder to append csv
<p>I have a ping sweeper that pulls back basic snmp info on any valid devices in the defined ip range, and appends these results to a stringbuilder csv but i seem to get some duplicates like the first three here:</p>

<pre><code>Date    IP  Site    MAC Manufacturer    Model   Serial
25/02/2016  10.65.10.8  CS  00:00:AA:C2:24:AC   Dell    Dell 7130cdn    XDH100611
25/02/2016  10.65.10.4  CS  00:00:AA:C2:24:AC   Dell    Dell 7130cdn    XDH100611
25/02/2016  10.65.10.3  CS  00:00:AA:C2:24:AC   Dell    Dell 7130cdn    XDH100611
25/02/2016  10.65.10.9  CS  00:00:AA:C2:3A:57   Dell    Dell 7130cdn    XDH100746
25/02/2016  10.65.10.11 CS  00:15:99:48:64:33   Dell    Dell 5330dn Mono Laser Printer  1ZG2FG1
25/02/2016  10.65.10.12 CS  00:15:99:45:93:70   Dell    Dell 5330dn Mono Laser Printer  2HC2FG1
</code></pre>

<p>it seems that as the multi threads are running sometimes the variables get updated in one thread and i end up with duplicates written to the csv. is there a better way to contain the variables to there own thread?  could i define the variables non globally? heres the sample code:</p>

<pre><code>static void Main(string[] args)
    {

        var header = string.Format("{0},{1},{2},{3},{4},{5},{6}{7}", "Date", "IP", "Site", "MAC", "Manufacturer","Model", "Serial", Environment.NewLine);
        csv.Append(header);

        string[] sites = { "65" };

        foreach (string site in sites)
        {
            string ipBase = "10." + site + ".10.";
            for (int i = 1; i &lt; 255; i++)
            {
                string ip = ipBase + i.ToString();
                Ping p = new Ping();
                p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
                countdown.AddCount();
                p.SendAsync(ip, 100, ip);
                Thread.Sleep(10);
            }
        }            
        File.WriteAllText("Printers.csv", csv.ToString());
        File.WriteAllText("Exceptions.csv", exception.ToString());
        Console.ReadLine();
    }

static void p_PingCompleted(object sender, PingCompletedEventArgs e)
    {
        string ip = (string)e.UserState;
        if (e.Reply != null &amp;&amp; e.Reply.Status == IPStatus.Success)
        {
            string host = ip;
            string community = "public";

            SimpleSnmp snmp = new SimpleSnmp(host, community);
            if (!snmp.Valid)
            {
                exception.Append(string.Format("{0},{1},{2}{3}", DateTime.Now.ToString("dd-MM-yy"), ip, "SNMP agent host name/ip address is invalid.", Environment.NewLine));
            }
            Dictionary&lt;Oid, AsnType&gt; result = snmp.Get(SnmpVersion.Ver1,
                                                      new string[] { ".1.3.6.1.2.1.1.1.0" });
            if (result == null)
            {
                exception.Append(string.Format("{0},{1},{2}{3}", DateTime.Now.ToString("dd-MM-yy"), ip, "no snmp result", Environment.NewLine));
            }
            else
            {
                    TRYONE(ip);
            }

            lock (lockObj)
            {
                upCount++;
            }
        }
    }

public static string WhatSite(string site)
    {
        string[] sitecode = site.Split('.');


        if (sitecode[1] == "65")
        {
            site = "CS";
            return site;
        }
        else
        {
            return "xx";
        }
    }

static void TRYONE (string ip)
    {
        string host = ip;
        string community = "public";
        model = "";
        serial = "";
        macaddress = "";

        SimpleSnmp snmp = new SimpleSnmp(host, community);

        if (!snmp.Valid)
        {
            exception.Append(string.Format("{0},{1},{2}{3}", DateTime.Now.ToString("dd-MM-yy"), ip, "SNMP agent host name/ip address is invalid.", Environment.NewLine));
            return;
        }
        Dictionary&lt;Oid, AsnType&gt; result = snmp.Get(SnmpVersion.Ver1,
            new string[] { ".1.3.6.1.2.1.25.3.2.1.3.1", ".1.3.6.1.2.1.43.5.1.1.17.1", ".1.3.6.1.4.1.11.2.4.3.1.23.0" });                                      



        if (result == null)
        {
                exception.Append(string.Format("{0},{1},{2}{3}", DateTime.Now.ToString("dd-MM-yy"), ip, "No results received?", Environment.NewLine));
                return;
        }
        else
        {
            foreach (KeyValuePair&lt;Oid, AsnType&gt; kvp in result)
            {
                if (kvp.Key.ToString() == "1.3.6.1.2.1.25.3.2.1.3.1")
                {
                    model = kvp.Value.ToString();
                    if (model.Contains(";"))
                    {
                        string[] temp = model.Split(';');
                        model = temp[0];
                    }                     
                }
                if (kvp.Key.ToString() == "1.3.6.1.2.1.43.5.1.1.17.1")
                {
                    serial = kvp.Value.ToString();

                }
                if (kvp.Key.ToString() == "1.3.6.1.4.1.11.2.4.3.1.23.0")
                {
                    macaddress = kvp.Value.ToString();
                    macaddress = macaddress.Replace(" ", ":");
                }

            }

            string[] manufacturer = model.Split(' ');

            Console.WriteLine("{0} {1} {2} {3} {4} {5}", ip, manufacturer[0], model, serial, macaddress, WhatSite(ip));

            csv.Append(string.Format("{0},{1},{2},{3},{4},{5},{6}{7}", DateTime.Now.ToString("dd-MM-yy"), ip, WhatSite(ip), macaddress, manufacturer[0], model, serial, Environment.NewLine));

        }
    }
</code></pre>

<p>Thanks</p>