-1

我的应用程序有问题。在 1 个线程中效果很好,但是当我更改线程 > 1 时它无法正常工作。

我有 3 个列表:

List<string> urls = new List<string>();
List<string> passwords = new List<string>();
    struct Proxy
    {
        public static List<string> proxies = new List<string>();
        public static string type;
    }

我需要所有线程都采用第一个 url,所有线程都采用第一个代理,每个线程都从列表中获取 unik(next) 密码。如果前 5 个线程采用 5 个密码,则代理更改为下一个,这些线程采用接下来的 5 个唯一通道。如何解决这个问题?

    Thread[] thr;
    static object locker = new object();
    int good_auth, bad_auth, j;

    private void button1_Click(object sender, EventArgs e)
    {
        decimal value = numericUpDown2.Value;
        int i = 0;
        int k = (int)(value);
        thr = new Thread[k];
        for (; i < k; i++)
        {
            thr[i] = new Thread(new ThreadStart(go));
            thr[i].IsBackground = true;
            thr[i].Start();
        }
    }

    public void go()
    {
        string urlANDlogin = "";
        for (int url_index = 0; url_index < urls.Count; url_index++)
        {
            urlANDlogin = urls[url_index];
            string proxy = "";
            string password = "";

            if (Proxy.proxies.Count != 0)
            {
                if (checkBox1.Checked)
                    Proxy.type = "http";
                else if (checkBox2.Checked)
                    Proxy.type = "socks5";
                else
                    Proxy.type = "none";
            }


            VB vb = new VB(urlANDlogin, Proxy.type);
            for (int proxy_index = 0; proxy_index < Proxy.proxies.Count; proxy_index++)
            {
                lock (locker)
                {
                    if (Proxy.proxies.Count == 0)
                    {
                        break;
                    }
                    else
                        proxy = Proxy.proxies[proxy_index];
                }
                vb.Proxy(proxy);
                for (int i = 0; i < 5; i++)
                {
                    lock (locker)
                    {
                        if (passwords.Count == 0)
                        {
                            break;
                        }
                        else
                        {
                            password = passwords[j];
                            j++;
                        }
                    }
                    string login = vb.Auth(password);
                    if (login == "Good")
                    {
                        lock (locker)
                        {
                            good_auth++;
                            log_good(good_auth);
                        }
                        break;
                    }
                    else
                    {
                        lock (locker)
                        {
                            bad_auth++;
                            log_bad(bad_auth);
                        }
                    }                            

                }
                lock (locker)
                {
                    log_left_proxy(Proxy.proxies.Count);
                    j = 0;
                }
            }
        }
    }
4

1 回答 1

1

我建议使用TPL 的Data Parallelism

就像是:

Parallel.ForEach(urls, url => Process(url));

除了所有其他注释之外,您不能从非 UI 线程访问 Control。因此,您必须在 click 方法中获取checkBox1and的值。checkBox2

于 2013-02-05T21:37:33.507 回答