本帖最后由 wuquaner4zx 于 2012-05-29 18:15:30 编辑

解决方案 »

  1.   

    Test_threads 里有没有东西,什么地方启动他们的?
      

  2.   

    begin end只是个参数,就是url的参数比如说http://www.xxx.com/index.php?id=(*),就是这个id在启动线程之前就已经初始化到dataTable里的url里面了,线程是直接到dt中读取的url,多少都可以的,数组的下标每次都是从0开始的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);
      

  3.   

    Thread.start() 失效具体是什么现象?
      

  4.   

    我调试的时候看到是这样的,就是这个函数单步过去了,但是这个我给这个线程初始化的方法并没有执行,就是说这个线程没有开启来。不知道为什么。还有如果我循环开启多个线程的话他最后只会运行结束2个线程,不管开多少个都运行结束,就是返回结果2个线程。其他的我调试的时候发现在执行res = hm.Post();之前是有的,执行完之后就只剩两个线程了。res = hm.Post();这个代码我现在没有,我刚换了电脑。这个就是httpwebrequest 发送post请求,但是我这个是封装到类里的呀,每个线程都是一个单独的对象,线程怎么会消失了呢?
      

  5.   

    http://topic.csdn.net/u/20120524/10/0588ab9d-5d2c-4698-8901-bb6333142d3c.html
     此贴回复下 谢谢!!!
      

  6.   

    1、多线程同步问题需要注意一下
    2、多个线程的话最好用线程池ThreadPool
    3、如果想循环执行某个任务,最好在线程函数内的while(true)中执行任务,在任务尾部Thread.Sleep(5000)等待5s后继续执行
      

  7.   

    经过调试发现this.req.ContentLength = paramBytes.Length;
                    this.req.ContentType = "application/x-www-form-urlencoded";
                    //Send request
                    Stream requestStream = this.req.GetRequestStream();
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                    requestStream.Close();
    到this.req.ContentType = "application/x-www-form-urlencoded";这行还是正常的,
    到Stream requestStream = this.req.GetRequestStream();行,就只剩2个线程了,这是为什么啊?
      

  8.   

    经过调试发现this.req.ContentLength = paramBytes.Length;
                    this.req.ContentType = "application/x-www-form-urlencoded";
                    //Send request
                    Stream requestStream = this.req.GetRequestStream();
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                    requestStream.Close();
    到this.req.ContentType = "application/x-www-form-urlencoded";这行,还是正常的。
    到Stream requestStream = this.req.GetRequestStream();这行,线程就只剩2个了!!!其他的就没有了?没有了!我勒个去,坑爹啊!什么意思啊?
      

  9.   

    我建议你的 Go_Thread 方法开始和结束的地方向控制台输出一下index,比如开头地方就 index + " start" ,结尾处就 index + " end" ,button1_Click_1 和 timer1_Tick 里输出一下 this.dt.Rows.Count ,不要断点,运行后看输出的内容,看看是不是真的线程丢失了。
      

  10.   

    MessageBox.Show("startStream" + url.ToString());
                    Stream requestStream= this.req.GetRequestStream();
                    MessageBox.Show("endStream" + url.ToString());
    就是这句话,Stream requestStream= this.req.GetRequestStream();这个不是丢失了,应该是阻塞了,因为程序退出了以后这个线程还在这里卡着,这要怎么办?
      

  11.   

    加上 Go_threads[i].IsBackground = true;
      

  12.   

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

  13.   

    找到问题了,要结贴了,我把问题代码段传上来大家看看 哪里不一样的就知道问题所在了。其实就是我在20楼说的问题。
    这是修改后的            try
                {
                    
                    //Send request                Stream requestStream= this.req.GetRequestStream();                
                    requestStream = this.req.GetRequestStream();
                    
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                    requestStream.Close();
                    
                    this.resp = (HttpWebResponse)this.req.GetResponse();
                    Encoding myEncoding = Encoding.Default;
                    if (this.resp.Headers["Content-Type"].Contains("utf-8"))
                    {
                        myEncoding = Encoding.GetEncoding("utf-8");
                    }
                    else if (this.resp.Headers["Content-Type"].Contains("GBK"))
                    {
                        myEncoding = Encoding.GetEncoding("GBK");
                    }
                    else if (this.resp.Headers["Content-Type"].Contains("GB2312"))
                    {
                        myEncoding = Encoding.GetEncoding("GB2312");
                    }
                   
                    StreamReader myStreamReader = new StreamReader(new GZipStream( resp.GetResponseStream(),CompressionMode.Decompress),myEncoding);                this.data = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                    ret_str = "success";
                    return ret_str;
                }这是之前的            try
                {
                    
                    //Send request                Stream requestStream= this.req.GetRequestStream();                
                    requestStream = this.req.GetRequestStream();
                    
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                    requestStream.Close();
                    
                    this.resp = (HttpWebResponse)this.req.GetResponse();
                    Encoding myEncoding = Encoding.Default;
                    if (this.resp.Headers["Content-Type"].Contains("utf-8"))
                    {
                        myEncoding = Encoding.GetEncoding("utf-8");
                    }
                    else if (this.resp.Headers["Content-Type"].Contains("GBK"))
                    {
                        myEncoding = Encoding.GetEncoding("GBK");
                    }
                    else if (this.resp.Headers["Content-Type"].Contains("GB2312"))
                    {
                        myEncoding = Encoding.GetEncoding("GB2312");
                    }
                   
                    StreamReader myStreamReader = new StreamReader(new GZipStream( resp.GetResponseStream(),CompressionMode.Decompress),myEncoding);                this.data = myStreamReader.ReadToEnd();
                    ret_str = "success";
                    return ret_str;
                }看出来的,请用板砖砸死我吧,我不想活了。真他妈的始终不能细心。纠结我啊!感谢大家关心我给我动力!我痛哭流涕……
      

  14.   

    myStreamReader.Close();
    不关闭,确实盖拍了
      

  15.   

    就多了一行: myStreamReader.Close();