c#多线程采集经常自动关闭,大家有遇到过吗?
                InitializeComponent();
                richTextBox1.ReadOnly = true;
                button2.Text = "清空";                textBox1.ReadOnly = true;
                Thread t1 = new Thread(new ThreadStart(MyThread));
                Thread t2 = new Thread(new ThreadStart(saishi));
                Thread t3 = new Thread(new ThreadStart(aosai));
                Thread t4 = new Thread(new ThreadStart(BodyData("infor_2.html","http://www.ups-cn.com/infor_2.php?u=",20000,"utf-8")));
                t1.Start();
                t2.Start();
                t3.Start();
                t4.Start();
以上是启动线程
        public void saishi() {
            for (int i = 1; i > 0; i++)
            {
                if (!File.Exists("saishi.html"))
                {
                    File.Create("saishi.html");
                }
                string status = null;
                string content = null;
                try
                {
                WebClient MyWebClient = new WebClient();
                Random rd = new Random();
                byte[] MyDataBuffer = MyWebClient.DownloadData("http://www.ups-cn.com/saishi.html?u=" + rd.Next(5, 555555555));                    content = Encoding.GetEncoding("utf-8").GetString(MyDataBuffer);
                    status = "采集成功";
                    File.WriteAllText("saishi.html", content);
                }
                catch (Exception e)
                {
                    status = "采集失败";
                }
                Control.CheckForIllegalCrossThreadCalls = false;                richTextBox1.Text += +i + "\t" + status + "\t" + "\thttp://www.ups-cn.com/saishi.html\t" + DateTime.Now.ToString() + "\n" + "内容:\n\t" + content + "\n--------------------------------------------------------\n";
                //  richTextBox1.Text += +i + "\t采集成功\t" + DateTime.Now.ToString() + "\n";
                Thread.Sleep(20000);
            }
        }这是其中的一个方法,这代码有问题吗,为什么软件有时候运行一两天才出现自动关闭,有时候一会就关了

解决方案 »

  1.   

    程序异常退出,不报异常是很难找出问题。
    你可以采用注释或修改主要代码来进行测试和收集一些运行期间的信息(也可以利用日志)例如你程序中的: Control.CheckForIllegalCrossThreadCalls = false;
    长时间的运行,也许会引发严重的问题。还有:byte[] MyDataBuffer = MyWebClient.DownloadData("http://www.ups-cn.com/saishi.html?u=" + rd.Next(5, 555555555));这些代码都有可能在长时间运行下
    出现意想不到的问题。
    你可以把程序拷贝出来一份,注释掉敏感的部分或采取添加运行日志的方法,试运行一段时间程序,看效果。
    也可以用专业测试软件来进行长时间运行测试。
      

  2.   

    异常了.  部分需要try catch的地方没有加而导致的
      

  3.   

    我把整个代码贴出来 你帮我看看
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Threading;
    using System.Windows.Forms;
    using System.IO;
    using System.Text.RegularExpressions;
    using Microsoft.Win32;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            string addurl = "";
            public Form1()
            {
                try
                {
                    InitializeComponent();
                    richTextBox1.ReadOnly = true;
                    button2.Text = "清空";                textBox1.ReadOnly = true;
                    Thread t1 = new Thread(new ThreadStart(MyThread));
                    Thread t2 = new Thread(new ThreadStart(saishi));
                    Thread t3 = new Thread(new ThreadStart(aosai));
                    Thread t4 = new Thread(new ThreadStart(BodyData));
                    t1.Start();
                    t2.Start();
                    t3.Start();
                    t4.Start();            }
                catch (Exception ee) { }
            }
            private string getHtml(string url)
            {                WebClient myWebClient = new WebClient();
                    byte[] myDataBuffer = myWebClient.DownloadData(url);
                    return Encoding.GetEncoding("utf-8").GetString(myDataBuffer);
                
            }
    //开机自动启动        private void Form1_Load(object sender, EventArgs e)
            {
                string KJLJ = Application.ExecutablePath;
                //MessageBox.Show(KJLJ);
                if (!File.Exists(KJLJ))
                    return;
                string newKJLJ = KJLJ.Substring(KJLJ.LastIndexOf("\\") + 1);
                RegistryKey Rkey =
                    Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (Rkey == null)
                    Rkey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");            Rkey.SetValue(newKJLJ, KJLJ);
            }
            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {        }/*-----------------------------------以下采集方法-----------------------------------------*/        public void MyThread() {
                for (int i = 1; i > 0; i++) {
                    if (i == 300) {
                        richTextBox1.Text = "";
                        i = 1;
                    }
                    if (!File.Exists("i.xml")) {
                        File.Create("i.xml");
                    }
                    string content = null;
                    string status = null;
                    try
                    {
                        WebClient MyWebClient = new WebClient();
                        Random rd = new Random();
                        byte[] MyDataBuffer = MyWebClient.DownloadData("http://www.ups-cn.com/i.xml?u=" + rd.Next(5, 555555555));
                                       
            
                        content = Encoding.GetEncoding("utf-8").GetString(MyDataBuffer);
                        status = "采集成功";
                        File.WriteAllText("i.xml",content);
                    }
                    catch (Exception e)
                    {
                        status = "采集失败";
                    }
                    Control.CheckForIllegalCrossThreadCalls = false;                richTextBox1.Text += +i+ "\t"+ status +"\t" + "\thttp://www.ups-cn.com/i.xml\t" + DateTime.Now.ToString() + "\n" + "内容:\n\t" + content +"\n--------------------------------------------------------\n";
                    //  richTextBox1.Text += +i + "\t采集成功\t" + DateTime.Now.ToString() + "\n";
                    Thread.Sleep(2000);
                }
            }
            public void BodyData() { 
                 for (int i = 1; i > 0; i++)
                {
                    if (!File.Exists("infor_2.html"))
                    {
                        File.Create("infor_2.html");
                    }
                    string status = null;
                    string content = null;
                    try
                    {
                    WebClient MyWebClient = new WebClient();
                    Random rd = new Random();
                    byte[] MyDataBuffer = MyWebClient.DownloadData("http://www.ups-cn.com/infor_2.php?u=" + rd.Next(5, 555555555));                    content = Encoding.GetEncoding("utf-8").GetString(MyDataBuffer);
                        status = "采集成功";
                        File.WriteAllText("infor_2.html", content);
                    }
                    catch (Exception e)
                    {
                        status = "采集失败";
                    }
                    Control.CheckForIllegalCrossThreadCalls = false;                richTextBox1.Text += +i + "\t" + status + "\t" + "\t***bodydata\t" + DateTime.Now.ToString() + "\n" + "内容:\n\t" + content + "\n--------------------------------------------------------\n";
                    Thread.Sleep(20000);
                }
            }
            public void aosai() {
                for (int i = 1; i > 0; i++)
                {
                    if (!File.Exists("predictions.xml"))
                    {
                        File.Create("predictions.xml");
                    }
                    string content = null;
                    string status = null;
                    try
                    {
                    WebClient MyWebClient = new WebClient();
                    Random rd = new Random();
                    byte[] MyDataBuffer = MyWebClient.DownloadData("http://www.ups-cn.com/odds/predictions.xml?u=" + rd.Next(5, 555555555));
                    
                                        content = Encoding.GetEncoding("Big5").GetString(MyDataBuffer);
                        status = "采集成功";
                        File.WriteAllText("predictions.xml", content);
                    }
                    catch (Exception e)
                    {
                        status = "采集失败";
                    }
                    Control.CheckForIllegalCrossThreadCalls = false;                richTextBox1.Text += +i + "\t" + status + "\t" + "\thttp://www.ups-cn.com/odds/predictions.xml\t" + DateTime.Now.ToString() + "\n" + "内容:\n\t" + content + "\n--------------------------------------------------------\n";
                    //  richTextBox1.Text += +i + "\t采集成功\t" + DateTime.Now.ToString() + "\n";
                    Thread.Sleep(2000000);
                }
            }
            public void saishi() {
                for (int i = 1; i > 0; i++)
                {
                    if (!File.Exists("saishi.html"))
                    {
                        File.Create("saishi.html");
                    }
                    string status = null;
                    string content = null;
                    try
                    {
                    WebClient MyWebClient = new WebClient();
                    Random rd = new Random();
                    byte[] MyDataBuffer = MyWebClient.DownloadData("http://www.ups-cn.com/saishi.html?u=" + rd.Next(5, 555555555));                    content = Encoding.GetEncoding("utf-8").GetString(MyDataBuffer);
                        status = "采集成功";
                        File.WriteAllText("saishi.html", content);
                    }
                    catch (Exception e)
                    {
                        status = "采集失败";
                    }
                    Control.CheckForIllegalCrossThreadCalls = false;                richTextBox1.Text += +i + "\t" + status + "\t" + "\thttp://www.ups-cn.com/saishi.html\t" + DateTime.Now.ToString() + "\n" + "内容:\n\t" + content + "\n--------------------------------------------------------\n";
                    //  richTextBox1.Text += +i + "\t采集成功\t" + DateTime.Now.ToString() + "\n";
                    Thread.Sleep(20000);
                }
            }
          
        }
    }
      

  4.   

    EventType clr20r3, P1 windowsformsapplication1.exe, P2 1.0.0.0, P3 4e5cde46, P4 system.windows.forms, P5 2.0.0.0, P6 4333aefa, P7 4e9e, P8 8b, P9 system.nullreferenceexception, P10 NIL. 
    以上是错误日志 请问这怎么看?
      

  5.   

    最笨的方法,先把所有线程运行的语句全部放在try...cath里面,找着问题所在了,再修改。
      

  6.   

    好像又是UI线程问题。richTextBox1.Text
      

  7.   

    问题出在这一行:Control.CheckForIllegalCrossThreadCalls = false;后台线程本不该访问UI线程创建的控件,访问操作将引发异常,而上面那句话将问题掩盖了,所以迟早会出问题。控件不是线程安全的,不应该跨线程调用。另外,应该学习一下编码规范,命名、排版习惯都不好,非托管资源用了也不释放。