1

大家好,我有这样的结构:启动线程:

 Thread[] thr;
    int good_auth, bad_auth, good_like, bad_like;
    static object accslocker = new object();
    static object limitlocker = new object();
    string acc_path, proxy_path, posts_path;
    int position_of_limit, position = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        button2.Enabled = true;
        decimal value = numericUpDown1.Value;
        int i = 0;
        int j = (int)(value);
        thr = new Thread[j];
        for (; i < j; i++)
        {
            thr[i] = new Thread(new ThreadStart(go));
            thr[i].IsBackground = true;
            thr[i].Start();
        }
    }

而不是功能去:

    public void go()
    {
        while (true)
        {
            string acc = "";
            string proxy = "";

            lock (limitlocker)
            {
                if (position_of_limit >= int.Parse(textBox2.Text) - 1)
                {
                    position_of_limit = 0;
                    if (position < posts.Count - 1)
                        position++;
                    else
                    {
                        break;
                    }
                }

            } 
            lock (accslocker)
            {
                if (accs.Count == 0)
                {
                    break;
                }
                else
                    acc = accs.Dequeue();
            }
            OD od = new OD(acc);
            string login = od.Auth();
            if (login == "Login")
            {
                lock (accslocker)
                {
                    good_auth++;
                    log_good_auth(good_auth);
                }

                string like = od.like(posts[position], textBox1.Text);
                if (like == "Good")
                {
                    lock (accslocker)
                    {
                        position_of_limit++;
                        good_like++;
                        log_good_like(good_like);
                    }
                }
                else if (like == "Failed")
                {
                    lock (accslocker)
                    {
                        bad_like++;
                        log_bad_like(bad_like);
                    }
                }
                else
                {
                    lock (accslocker)
                    {
                        bad_like++;
                        log_bad_like(bad_like);
                    }
                }

            }
            else if (login == "Spamblock")
            {
                lock (accslocker)
                {
                    bad_auth++;
                    log_bad_auth(bad_auth);
                }
            }
            else if (login == "Locked")
            {
                lock (accslocker)
                {
                    bad_auth++;
                    log_bad_auth(bad_auth);
                }
            }
            else if (login == "Invalid")
            {
                lock (accslocker)
                {
                    bad_auth++;
                    log_bad_auth(bad_auth);
                }
            }
            else if (login == "Bad_proxy")
            {
                lock (accslocker)
                {
                    accs.Enqueue(acc);
                    Proxy.proxies.Remove(proxy); 
                }
            }
            else
            {
                lock (accslocker)
                {
                    accs.Enqueue(acc);
                    Proxy.proxies.Remove(proxy);
                }
            }
        }
    }

例如,我开始 20 个线程,当 position_of_limit 变得大于 int.Parse(textBox2.Text) - 1 所有线程都需要在下一个循环中获取下一个帖子[位置]。但是我在行字符串上收到一个异常,例如 = od.like(posts[position], textBox1.Text);

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

如何解决这个问题呢?谢谢

4

1 回答 1

0

锁定 GUI 线程是邪恶的,因为它是一个 STA 线程。这意味着它必须管理一个消息循环并且不允许阻塞。所以阻塞很可能导致死锁。

而是使用回调,例如BackgroundWorker.RunWorkerCompleted.

有关锁定的其他一些信息链接:

于 2013-07-10T20:59:26.803 回答