锁的多线程问题

时间:2013-07-10 20:29:07

标签: c#

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

 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();
        }
    }

而不是功能go:

    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所有线程需要在下一个循环中接下一个帖子[position]。但我在行字符串上收到一个异常,如= 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"

如何解决这个问题?感谢

1 个答案:

答案 0 :(得分:0)

锁定GUI线程是 evil ,因为它是一个STA线程。 这意味着它必须管理消息循环并且不允许阻止。因此阻塞可能会导致死锁。

而是使用BackgroundWorker.RunWorkerCompleted.

之类的回调

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