日期:2014-05-18  浏览次数:20959 次

跪求…………………………跪求指导…………我要暴走了……
首先,我通过按钮启动多个线程,循环启动,但是每次都是只能运行完成2个线程
C# code


     private void button1_Click_1(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
            this.Paraments = this.textBox1.Text;
            this.Cookies = this.textBox2.Text;
            if (this.dt.Rows.Count == 0)
            {
                for (int i = begin; i <= end; i++)
                {

                    DataRow dr = dt.NewRow();
                    dr["URL"] = this.text_url.Text.Replace("(*)", i.ToString());
                    dr["id"] = i.ToString();
                    this.dt.Rows.Add(dr);
                }
                this.dataGridView1.Refresh();
            }
            StopThread(Go_threads);

            for (int i = 0; i < this.dt.Rows.Count; i++)
            {
                Go_threads[i] = new Thread(new ParameterizedThreadStart(Go_Thread));
                //Go_threads[i].Name = "thread_" + i.ToString();
                Go_threads[i].Start(i);
            }
            
        }
        private void Go_Thread(object index)
        {
            int i = int.Parse(index.ToString());
            string Url = dt.Rows[i]["URL"].ToString();
            string parament = Paraments;
            parament = parament.Replace("(*)", dt.Rows[i]["id"].ToString());
            string cookies = Cookies;
            HttpManager hm = new HttpManager(Url,parament,cookies);
            string res = "";

            res = hm.Post();
            string html = hm.Data;
            if (html != string.Empty)
            {
                if (html.Contains("文章不存在"))
                {
                    res = "HasNotpage";
                }
            }
            dt.Rows[i]["FLAG"] = res;


        }



我加入了一个timer,然后设置5秒检测一次线程数组,如果数组没有running线程的话,就新开下一组线程,但是我发现在这个时候,Thread.start()失效了!!!!求解啊啊啊啊啊啊啊!!!!

C# code

        private void timer1_Tick(object sender, EventArgs e)
        {
            bool isOver = true;
            for (int i = 0; i < Test_threads.Length; i++)
            {
                if (Test_threads[i] != null)
                {
                    if (Test_threads[i].IsAlive)
                    {
                        if (Test_threads[i].ThreadState != ThreadState.Stopped)
                        {
                            isOver = false;
                            break;
                        }
                    }
                }
            }
            for (int i = 0; i < Go_threads.Length; i++)
            {
                if (Go_threads[i] != null)
                {
                    if (Go_threads[i].IsAlive)
                    {
                        if (Go_threads[i].ThreadState != ThreadState.Stopped)
                        {
                            isOver = false;
                            break;
                        }
                    }
                }
            }
            if (isOver)
            {
                this.text_begin.Text = (int.Parse(this.text_begin.Text) - 2).ToString();
                
                dt.Clear();
                if (this.dt.Rows.Count == 0)
                {
                    for (int i = begin; i <= end; i++)
                    {

                        DataRow dr = dt.NewRow();
                        dr["URL"] = this.text_url.Text.Replace("(*)", i.ToString());
                        dr["id"] = i.ToString();
                        this.dt.Rows.Add(dr);
                    }
                    this.dataGridView1.Refresh();
                }
                StopT