UI线程问题

csdn_风中雪狼 2009-04-24 07:16:12
写了一个分析网址的程序,由于程序长时间去分析网址,所以呢导致窗体界面长时间没有反应,造成假死现象,请问各位高人,这种情况该怎么去处理。
...全文
96 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_风中雪狼 2009-04-24
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
private string My_ConnStr = string.Empty;
private void button1_Click(object sender, EventArgs e)//开始按钮
{
DataSet dt;
dt = new DataSet();
dt =getUrl("select top 500 url from url where selectflag='1'");

Int32 li_Count = dt.Tables[0].Rows.Count;

for (Int32 li_i = 0; li_i < li_Count; li_i++)
{
getHtmlMess(dt.Tables[0].Rows[li_i][0].ToString().Trim());
}
dt.Dispose();
}

private void getHtmlMess(string ingurl)
{
string page = Gethtml(ingurl);

if (page.Length == 0)
{
return; //连接超时,就返回
}

Regex re = new Regex(@"href=(? <web_url>[\s\S]*?)>|href=""(? <web_url>[\s\S]*?)""|href='(? <web_url>[\s\S]*?)'");
MatchCollection mc = re.Matches(page);
foreach (Match m in mc)
{
string url = m.Groups["web_url"].ToString().Trim();

if ((url.IndexOf("'") == 0) || (url.IndexOf("\"") == 0))//去除头部的'与"
{
url = url.Remove(0, 1);
if (url.IndexOf("'") != -1)
{
url = url.Remove(url.IndexOf("'"), 1);
}
if (url.IndexOf("\"") != -1)
{
url = url.Remove(url.IndexOf("\""), 1);
}
}
if (url.IndexOf(" ") != -1)
{
url = url.Remove(url.IndexOf(" "));
}
if ((url.Length < 6) || (url.IndexOf("\"") > 0)) { continue; }

if ((url.Substring(url.Length - 4, 4).ToUpperInvariant() == ".JPG") || (url.Substring(url.Length - 4, 4).ToUpperInvariant() == ".GIF") || (url.Substring(url.Length - 4, 4).ToUpperInvariant() == ".CSS"))
{
continue;
}
if (url.IndexOf("blogs.ebay") > 0) { continue; }


if (url.IndexOf("http://") != -1)
{
listView1.Items.Add(url);
}
}
}


/// <summary>
///取得html代码
/// </summary>
/// <param name="url">指定的Url </param>
/// <returns>返回指定Url的Html </returns>
private string Gethtml(string url)
{
try
{
string html = string.Empty;
string encoding = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "get";
request.ContentType = "text/html";
request.Timeout = 30 * 1000;
byte[] buffer = new byte[2048];
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{

using (Stream reader = response.GetResponseStream())
{
reader.ReadTimeout = 30 * 1000;
using (MemoryStream memory = new MemoryStream())
{
int index = 1;
int sum = 0;
while (index > 0 && sum < 100 * 2048)
{
index = reader.Read(buffer, 0, 2048);
if (index > 0)
{
memory.Write(buffer, 0, index);
sum += index;
}
}
html = Encoding.GetEncoding("utf-8").GetString(memory.ToArray());

if (string.IsNullOrEmpty(html))
{
return html;
}
else
{
Regex re = new Regex(@"charset=(? <charset>[\s\S]*?)[""|']");
Match m = re.Match(html.ToLower());
encoding = m.Groups["charset"].ToString();
}

if (string.IsNullOrEmpty(encoding) || string.Equals(encoding.ToLower(), "utf-8"))
{
return html;
}
else
{
return Encoding.GetEncoding(encoding).GetString(memory.ToArray());
}
}
}
}
}
catch
{
return "";
}
}

/// 执行传入的SQl语句,将查询结果通过数据集返回给调用者
/// </summary>
/// <sql>一条可执行的SQL </sql>
/// <returns>Dataset </returns>
private DataSet getUrl(string Sql)
{
OleDbConnection conn = new OleDbConnection();
DataSet myData = new DataSet();
try
{
conn.ConnectionString = My_ConnStr;
OleDbDataAdapter MyDataAd = new OleDbDataAdapter(Sql, conn);
MyDataAd.Fill(myData, "SpiderTable");
MyDataAd.Dispose();
}
catch (Exception err)
{
if (err.Message.ToString() == "[DBNETLIB][ConnectionOpen (Connect()).]SQL Server 不存在或拒绝访问。")
{
MessageBox.Show("数据库连接失败,本系统将终止执行,请确认数据库服务器是否开启!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.ExitThread();
Application.Exit();
conn.Dispose();
return myData;
}
}

finally
{
conn.Dispose();
}
return myData;
}

private void Form1_Load(object sender, EventArgs e)
{
My_ConnStr = textBox3.Text.ToString();
}
}
}

csdn_风中雪狼 2009-04-24
  • 打赏
  • 举报
回复
其代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
private string My_ConnStr = string.Empty;
private void button1_Click(object sender, EventArgs e)//开始按钮
{
DataSet dt;
dt = new DataSet();
dt =getUrl("select top 500 url from url where selectflag='1'");

Int32 li_Count = dt.Tables[0].Rows.Count;

for (Int32 li_i = 0; li_i < li_Count; li_i++)
{
getHtmlMess(dt.Tables[0].Rows[li_i][0].ToString().Trim());
}
dt.Dispose();
}

private void getHtmlMess(string ingurl)
{
string page = Gethtml(ingurl);

if (page.Length == 0)
{
return; //连接超时,就返回
}

Regex re = new Regex(@"href=(?<web_url>[\s\S]*?)>|href=""(?<web_url>[\s\S]*?)""|href='(?<web_url>[\s\S]*?)'");
MatchCollection mc = re.Matches(page);
foreach (Match m in mc)
{
string url = m.Groups["web_url"].ToString().Trim();

if ((url.IndexOf("'") == 0) || (url.IndexOf("\"") == 0))//去除头部的'与"
{
url = url.Remove(0, 1);
if (url.IndexOf("'") != -1)
{
url = url.Remove(url.IndexOf("'"), 1);
}
if (url.IndexOf("\"") != -1)
{
url = url.Remove(url.IndexOf("\""), 1);
}
}
if (url.IndexOf(" ") != -1)
{
url = url.Remove(url.IndexOf(" "));
}
if ((url.Length < 6) || (url.IndexOf("\"") > 0)) { continue; }

if ((url.Substring(url.Length - 4, 4).ToUpperInvariant() == ".JPG") || (url.Substring(url.Length - 4, 4).ToUpperInvariant() == ".GIF") || (url.Substring(url.Length - 4, 4).ToUpperInvariant() == ".CSS"))
{
continue;
}
if (url.IndexOf("blogs.ebay") > 0) { continue; }


if (url.IndexOf("http://") != -1)
{
listView1.Items.Add(url);
}
}
}


/// <summary>
///取得html代码
/// </summary>
/// <param name="url">指定的Url</param>
/// <returns>返回指定Url的Html</returns>
private string Gethtml(string url)
{
try
{
string html = string.Empty;
string encoding = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "get";
request.ContentType = "text/html";
request.Timeout = 30 * 1000;
byte[] buffer = new byte[2048];
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{

using (Stream reader = response.GetResponseStream())
{
reader.ReadTimeout = 30 * 1000;
using (MemoryStream memory = new MemoryStream())
{
int index = 1;
int sum = 0;
while (index > 0 && sum < 100 * 2048)
{
index = reader.Read(buffer, 0, 2048);
if (index > 0)
{
memory.Write(buffer, 0, index);
sum += index;
}
}
html = Encoding.GetEncoding("utf-8").GetString(memory.ToArray());

if (string.IsNullOrEmpty(html))
{
return html;
}
else
{
Regex re = new Regex(@"charset=(?<charset>[\s\S]*?)[""|']");
Match m = re.Match(html.ToLower());
encoding = m.Groups["charset"].ToString();
}

if (string.IsNullOrEmpty(encoding) || string.Equals(encoding.ToLower(), "utf-8"))
{
return html;
}
else
{
return Encoding.GetEncoding(encoding).GetString(memory.ToArray());
}
}
}
}
}
catch
{
return "";
}
}

/// 执行传入的SQl语句,将查询结果通过数据集返回给调用者
/// </summary>
/// <sql>一条可执行的SQL</sql>
/// <returns>Dataset</returns>
private DataSet getUrl(string Sql)
{
OleDbConnection conn = new OleDbConnection();
DataSet myData = new DataSet();
try
{
conn.ConnectionString = My_ConnStr;
OleDbDataAdapter MyDataAd = new OleDbDataAdapter(Sql, conn);
MyDataAd.Fill(myData, "SpiderTable");
MyDataAd.Dispose();
}
catch (Exception err)
{
if (err.Message.ToString() == "[DBNETLIB][ConnectionOpen (Connect()).]SQL Server 不存在或拒绝访问。")
{
MessageBox.Show("数据库连接失败,本系统将终止执行,请确认数据库服务器是否开启!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.ExitThread();
Application.Exit();
conn.Dispose();
return myData;
}
}

finally
{
conn.Dispose();
}
return myData;
}

private void Form1_Load(object sender, EventArgs e)
{
My_ConnStr = textBox3.Text.ToString();
}
}
}
csdn_风中雪狼 2009-04-24
  • 打赏
  • 举报
回复
由于是刚注册的,
所以没有可用的分数
还请各位高人相助

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧