读书人

多线程有关问题

发布时间: 2012-01-18 00:23:26 作者: rapoo

多线程问题
DataTable dt = new DataTable();
Thread thread;
delegate void bindgridview();
private void MainForm_Load(object sender, EventArgs e)
{
//在DataTable中添加列
dt.Columns.Add("名称", System.Type.GetType("System.String"));
dt.Columns.Add("地址", System.Type.GetType("System.String"));
dt.Columns.Add("电话", System.Type.GetType("System.String"));
dt.Columns.Add("地址经纬度", System.Type.GetType("System.String"));
}

//根据Url地址得到网页的html源码
private string GetWebContent(string Url)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//声明一个HttpWebRequest请求
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("utf-8");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
//关闭读取流对象
streamReader.Close();
}
catch
{
// MessageBox.Show("请求出错。。。");
}
return strResult;
}


//向dataGridView中加载数据
private void BindList()
{

string webUrl;
if (this.txtUrl.Text.Trim() == "")
{
// 要抓取的URL地址
webUrl = "http://www.dianping.com/search/category/2/10/g311";

}
else
{
webUrl = this.txtUrl.Text.Trim();

}
//得到指定Url的源码
string strContent = GetWebContent(webUrl);
//把换行符和空格符过滤掉
strContent = strContent.Replace("\r", "").Replace("\n", "");
rchCode.Text = strContent;
//获取商家的总数量
int count = Convert.ToInt32(GetSubString(ref strContent, "class=\"Color7\">(", ")"));

//保存商家信息的最大页数
int maxPage = 0;
if (count % 15 == 0)
{
maxPage = count / 15;
}
else
{
maxPage = (count / 15) + 1;
}
// 先清空
dt.Rows.Clear();

for (int i = 1; i <= 50; i++)
{
GetDataByUrl(webUrl + "p" + i);
}

// 把获取的结果绑定到dataGridView上
this.dgvDetails.DataSource = dt;
}
private void BindThread()
{
ThreadStart bind = new ThreadStart(BindList);
thread = new Thread(bind);
thread.Start();
}

private void CheckIsIn()


{
if (this.dgvDetails.InvokeRequired)
{
bindgridview d = new bindgridview(BindThread);
this.dgvDetails.Invoke(d);
}
else
{
BindThread();
}
}
private void btnShow_Click(object sender, EventArgs e)
{
this.dgvDetails.AutoGenerateColumns = false;

CheckIsIn();
}
这个程序不能运行,老是说无法将文件“obj\x86\Debug\Factory.exe”复制到“bin\Debug\Factory.exe”。文件“bin\Debug\Factory.exe”正由另一进程使用,因此该进程无法访问此文件。请各位高手看看是怎么回事?

[解决办法]
//加个委托
delegate void DelegateSetText(string text);

private void BindList()
{

string webUrl;
if (this.txtUrl.Text.Trim() == "")
{
// 要抓取的URL地址
webUrl = "http://www.dianping.com/search/category/2/10/g311";

}
else
{
webUrl = this.txtUrl.Text.Trim();

}
//得到指定Url的源码
string strContent = GetWebContent(webUrl);
//把换行符和空格符过滤掉
strContent = strContent.Replace("\r", "").Replace("\n", "");
SetText(strContent); //这里修改了
//获取商家的总数量
int count = Convert.ToInt32(GetSubString(ref strContent, "class=\"Color7\">(", ")"));

//保存商家信息的最大页数
int maxPage = 0;
if (count % 15 == 0)
{
maxPage = count / 15;
}
else
{
maxPage = (count / 15) + 1;
}
// 先清空
dt.Rows.Clear();
//循环获取每页的信息
//for (int i = 1; i <=maxPage; i++)
for (int i = 1; i <= 3; i++)
{
GetDataByUrl(webUrl + "p" + i);
}

// 把获取的结果绑定到dataGridView上
CheckIsIn(); //这里修改了
}

private void CheckIsIn()
{
if (this.dgvDetails.InvokeRequired)
{
bindgridview d = new bindgridview(CheckIsIn);
this.dgvDetails.Invoke(d);
}
else
{
this.dgvDetails.DataSource = dt;
}
}

private void SetText(string text)
{
if (this.rchCode.InvokeRequired)
{
DelegateSetText d = new DelegateSetText(SetText);
this.rchCode.Invoke(d, new object[] { text});
}
else
{
this.rchCode.Text = text;
}
}

//点击按钮显示信息
private void btnShow_Click(object sender, EventArgs e)
{
this.dgvDetails.AutoGenerateColumns = true;
//CheckIsIn();
BindThread();
}


[解决办法]

探讨

引用:
也可以考虑使用BackgroundWorker处理此问题
http://baike.baidu.com/view/3160326.htm

恩,现在加载数据的时候窗体不会出现假死状态,但是数据全部加载完任然非常缓慢,用了进度条,但是进度条是数据加载完了之后才显示的,这和没有进度条没什么区别,为什么用了多线程数据加载还是非常缓慢了?

[解决办法]
探讨

引用:
引用:

引用:
也可以考虑使用BackgroundWorker处理此问题


http://baike.baidu.com/view/3160326.htm

恩,现在加载数据的时候窗体不会出现假死状态,但是数据全部加载完任然非常缓慢,用了进度条,但是进度条是数据加载完了之后……

读书人网 >.NET Framework

热点推荐