繁体   English   中英

C#多线程不开始工人/工作?

[英]C# Multi threading not starting worker/job?

由于某种原因,我的线程无视我的工作,看来任何人都知道为什么要这样做吗? 非常感谢您的帮助,非常感谢。 这个程序是ProxyChecker,因为我买了一堆,并且将继续使用具有不同用户/通行证等的代理进行操作,但是其中一些已过期

        static List<String> user = new List<String>();
        static List<String> pass = new List<String>();
        static List<String> ips = new List<String>();
    static Random rnd = new Random();

    static void Main(string[] args)
    {
        int threads = 4;
        loadips();
        Console.WriteLine("Enter the amount of threads to run (4 default):");
        threads = Int32.Parse(Console.ReadLine());
        Console.WriteLine("Starting on " + threads + " threads...");
        for (int i = 0; i < threads; i++)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(CheckProxy), i);
        }
        //Console.ReadLine();
    }
    public class MyIP
    {
        public string IP { get; set; }
        public bool AcceptsConnection { get; set; }
    }

    private static void CheckProxy(object state)
    {
        var u = user[0];
        var p = pass[0];
        var l = new List<MyIP>();
        Parallel.ForEach(l.ToArray(), (item) =>
        {
            string ip = getip();
            try
            {
                using (var client = new ProxyClient(ip, u, p))
                {
                    Console.WriteLine(ip, user, pass);
                    client.Connect();
                    item.AcceptsConnection = client.IsConnected;
                }
            }
            catch
            {
                l.Remove(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");
        }
    }



 private static void loadips()
        {
            using (TextReader tr = new StreamReader("ips.txt"))
            {
                string line = null;
                while ((line = tr.ReadLine()) != null)
                {
                    ips.Add(line);
                }
            }
        }

您不理解我的代码示例...您应该将IP添加到列表l ,而不是使用getip()方法...因此丢失了getip()方法

private static void CheckProxy(object state)
{
    var u = user[0];
    var p = pass[0];
    var l = new List<MyIP>();

    l.Add(new MyIP { IP = "192.168.1.1" });
    l.Add(new MyIP { IP = "192.168.1.2" });
    l.Add(new MyIP { IP = "192.168.1.3" });


    Parallel.ForEach(l.ToArray(), (ip_item) =>
    {
        try
        {
            using (var client = new ProxyClient(ip_item, u, p))
            {
                Console.WriteLine(ip_item, user, pass);
                client.Connect();
                item.AcceptsConnection = client.IsConnected;
            }
        }
        catch
        {
            lock(l)
                l.Remove(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");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM