csharpnoobie
csharpnoobie

Reputation: 21

C# threading parallel issue

Help is very welcome and extremely appreciated, thank you. What this program is, is a ProxyChecker, because i've bought a bunch and will continue to do so of proxies with different user/passes etc, however some have expired. I added a break point and what it's doing is actually skipping the ProxyClient code and going straight to for each var item in 1, if item accepts connection etc, it then just returns false and finishes.

private static void CheckProxy(object state)
{
    var u = user[0];
    var p = pass[0];
    var l = new List<MyIP>();
    Parallel.ForEach(l.ToArray(), (ip_item) =>
    {
        try
        {
            string ip = ip_item.IP;
            using (var client = new ProxyClient(ip, u, p))
            {
                Console.WriteLine(ip, user, pass);
                client.Connect();
                ip_item.AcceptsConnection = client.IsConnected;
            }
        }
        catch
        {
            l.Remove(ip_item);
        }
    });
    foreach (var item in l)
    {
        if (item.AcceptsConnection == true)
        {
            WriteToFile(user[0], pass[0]);
        }
        Console.WriteLine(item.IP + " is " + (item.AcceptsConnection) + " accepts connections" + " doesn not accept connections");
    }
}

Load ips function:#

private static void loadips()
        {
            using (TextReader tr = new StreamReader("ips.txt"))
            {
                var l = new List<MyIP>();
                string line = null;
                while ((line = tr.ReadLine()) != null)
                {
                    l.Add(new MyIP { IP = line });
                }
            }
        }

I have added this in response to the answer. I believe this is a variable issue as the variable is locally declared not publicly any ideas how to fix? i'm unable to find a way to get this working seems like i'm being dumb. thanks.

Upvotes: 0

Views: 43

Answers (1)

Tim Copenhaver
Tim Copenhaver

Reputation: 3302

The problem is in these two lines:

var l = new List<MyIP>();
Parallel.ForEach(l.ToArray(), (ip_item) =>

You just created l as a new List with no items in it. Calling ToArray() will give you an empty array. When Parallel.ForEach sees an empty array, it just gets skipped since there's nothing to iterate over.

Upvotes: 1

Related Questions