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);
}
}这是其中的一个方法,这代码有问题吗,为什么软件有时候运行一两天才出现自动关闭,有时候一会就关了
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);
}
}这是其中的一个方法,这代码有问题吗,为什么软件有时候运行一两天才出现自动关闭,有时候一会就关了
你可以采用注释或修改主要代码来进行测试和收集一些运行期间的信息(也可以利用日志)例如你程序中的: Control.CheckForIllegalCrossThreadCalls = false;
长时间的运行,也许会引发严重的问题。还有:byte[] MyDataBuffer = MyWebClient.DownloadData("http://www.ups-cn.com/saishi.html?u=" + rd.Next(5, 555555555));这些代码都有可能在长时间运行下
出现意想不到的问题。
你可以把程序拷贝出来一份,注释掉敏感的部分或采取添加运行日志的方法,试运行一段时间程序,看效果。
也可以用专业测试软件来进行长时间运行测试。
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);
}
}
}
}
以上是错误日志 请问这怎么看?