多线程问题

本文关键字:问题 多线程 | 更新日期: 2023-09-27 17:57:43

我的应用程序有问题。在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(下一个)密码。若前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;
                }
            }
        }
    }

多线程问题

我建议使用TPL中的数据并行性。

类似于:

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

除了所有其他注释之外,您不能从非UI线程访问控件。因此,您必须在单击方法中获得checkBox1checkBox2的值。