首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

跪求…跪求指导…小弟我要暴走了…

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

跪求…………………………跪求指导…………我要暴走了……
首先,我通过按钮启动多个线程,循环启动,但是每次都是只能运行完成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();                }                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);                }            }        } 


求你们了……看一下吧……


[解决办法]
begin end 各是多少?
[解决办法]
新开的线程会有滞后,如果运行速度过快的话,线程任务尚未完成呢,所以,开线程应该慎重考虑,或用同步委托。
[解决办法]
1、多线程同步问题需要注意一下
2、多个线程的话最好用线程池ThreadPool
3、如果想循环执行某个任务,最好在线程函数内的while(true)中执行任务,在任务尾部Thread.Sleep(5000)等待5s后继续执行
[解决办法]
我建议你的 Go_Thread 方法开始和结束的地方向控制台输出一下index,比如开头地方就 index + " start" ,结尾处就 index + " end" ,button1_Click_1 和 timer1_Tick 里输出一下 this.dt.Rows.Count ,不要断点,运行后看输出的内容,看看是不是真的线程丢失了。
[解决办法]
你的程序结构很不合理,写的人会疯,用的人也会疯的。
[解决办法]
可以一个一个执行啊,不能让他们同时执行,加锁之类的

[解决办法]
你能确保在timer访问时,线程数组中的线程都有效吗,如果一个线程执行完毕,会自动结束的

热点排行