ASP.NET多线程总问题
protected void btn_Submit_Click(object sender, EventArgs e)
{
Response.Buffer = false;
ThreadStart startDownload = new ThreadStart(Do_Data);
Thread oThread = new Thread(startDownload);
oThread.Name = "线程式";
oThread.Start();
oThread.Abort();
Response.Write("抓取完毕,线程终止");
Response.End();
}
private void Do_Data()
{
EC.GetRemoteObj o = new EC.GetRemoteObj();
int cc = 1;
for (int j = int.Parse(UPpage.Text); j <= int.Parse(next_Pata.Text); j++)
{
//string Url = "http://search.51job.com/list/" + Request.Form["LocusArea"] + ",0000,"+ddl_JobType.SelectedValue+",00,9,99,+,2," + j + ".html?lang=c&stype=2&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=01&lonlat=0%2C0&radius=-1&ord_field=0&list_type=1";//URL拼接
string Url = "http://search.51job.com/list/" + Request.Form["LocusArea"] + ",0000," + ddl_JobType.SelectedValue + ",00,9,99,+,2," + j + ".html?lang=c&stype=2&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=01&lonlat=0%2C0&radius=-1&ord_field=0&list_type=1";
string HtmlCode = o.GetRemoteHtmlCode(Url);
string s = o.GetHref(HtmlCode);
s = s.Replace("||", "|");
string[] arr = s.Split('|');
string Url_file = "";
string[] LocusArea = new string[2];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i].IndexOf("http://search.51job.com/job/") != -1)
{
//Random rand = new Random();
//int Sleeptime_Int = rand.Next(int.Parse(this.Thread_dll.SelectedValue));
//System.Threading.Thread.Sleep(Sleeptime_Int);
//Response.Write("延长时间抓取:" + Sleeptime_Int + "<br>");
try
{
Url_file = arr[i] + ",c.html";
Url_file = Url_file.Replace("\"", "");
// Response.Write(Url_file);
// Url_file = "http://search.51job.com/job/40602779,c.html";
string HtmlCode_List = o.ReplaceEnter(o.GetRemoteHtmlCode_x(Url_file));
com.Username = GetRandomString(8);
com.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(com.Username, "md5");
string s_List = o.Rec_Str(o.ReplaceEnter(HtmlCode_List), "Mail", "mail").ToLower().Replace(" ", "");
string reg = "";
reg = "公司网站.+?(" + o.Get_Reg() + ")";
com.WebSite = o.Get_M_Str(o.RemoveHTML(o.GetRegValue(reg, s_List)));
reg = "地 址.+?(" + o.Get_Reg() + ")";
com.Address = o.Get_M_Str(o.RemoveHTML(o.GetRegValue(reg, s_List)));
reg = "邮政编码.+?(" + o.Get_Reg() + ")";
com.PostCode = o.RemoveHTML(o.GetRegValue(reg, s_List));
com.PostCode = o.Get_M_Str(com.PostCode);
reg = "传 真.+?(" + o.Get_Reg() + ")";
com.Fax = o.RemoveHTML(o.GetRegValue(reg, s_List));
com.Fax = o.Get_M_Str(com.Fax);
reg = "联 系 人.+?(" + o.Get_Reg() + ")";
com.Linkman = o.RemoveHTML(o.GetRegValue(reg, s_List));
com.Linkman = o.Get_M_Str(com.Linkman);
reg = "电 话.+?(" + o.Get_Reg() + ")";
com.Phone = o.RemoveHTML(o.GetRegValue(reg, s_List));
com.Phone = o.Get_M_Str(com.Phone);
reg = "电子邮箱.+?(" + o.Get_Reg() + ")";
com.Email = o.RemoveHTML(o.GetRegValue(reg, s_List));
com.Email = o.Get_M_Str(com.Email).Replace(" ", "");
if (com.Email == "" || com.Email.Length < 0)
{
continue;
}
reg = "<strongstyle=font-size:14px>.+?</strong>";
com.CompanyName = o.RemoveHTML(o.GetRegValue(reg, s_List));
com.CompanyName = o.Get_M_Str(com.CompanyName);
if (s_List.IndexOf("公司规模:") == -1)
{
reg = "公司行业:</strong>.+?<br><br><strong>";
}
else
{
reg = "公司行业:</strong>.+? <strong>";
}
com.Trade = o.RemoveHTML(o.GetRegValue(reg, s_List).Replace("公司行业:", "").Replace(" ", ""));
com.Trade = com.Trade.Replace(" ", "|");
com.Trade = o.Get_M_Str(com.Trade);
string[] Trader = com.Trade.Split('|');
if (Trader.Length > 1)
{
if (Trader[0] != "")
{
com.Trade = o.Get_Trade(Trader[0]);
}
else
{
com.Trade = o.Get_Trade(Trader[1]);
}
}
else
{
com.Trade = o.Get_Trade(Trader[0]);
}
reg = "公司性质:</strong>.+?<br><br>";
com.Property = o.RemoveHTML(o.GetRegValue(reg, s_List));
com.Property = o.Get_prop(o.Get_M_Str(com.Property));
reg = "公司规模:</strong>.+?</td>";
com.Employee = o.RemoveHTML(o.GetRegValue(reg, s_List));
if (com.Employee != "")
{
com.Employee = o.Get_EM(o.Get_M_Str(com.Employee));
}
else
{
com.Employee = "1000";
}
reg = "<pclass=txt_font>.+?</p>";
com.CompanyMemo = o.Get_To_Html(o.RemoveHTML(o.Get_Html(o.GetRegValue(reg, s_List))));
com.CompanyMemo = com.CompanyMemo;
reg = "工作地点.+?</td>.+?</td>";
com.LocusArea = o.RemoveHTML(o.GetRegValue(reg, s_List));
com.LocusArea = o.Get_M_Str(com.LocusArea);
LocusArea = com.LocusArea.Split('-');
com.LocusArea = o.Get_Place(LocusArea[0], 1);
if (LocusArea.Length > 1)
{
com.LocusCity = o.Get_Place(LocusArea[1], 2);
}
string Com_Id = com.Insert_Com();
reg = "<!--topend-->.+?职位描述";
string b_List = o.GetRegValue(reg, s_List);
ob.Comid = Com_Id;
ob.Username = com.Str_Username(Com_Id);
reg = @"<tdclass=sr_btcolspan=2>.+?</td>";
ob.JobName = o.RemoveHTML(o.GetRegValue(reg, b_List));
if (ob.JobName == "")
{
continue;
}
ob.JobProperty = "1";
ob.Department = "0";
reg = "招聘人数:+?.+?</td>";
ob.Mans = o.Get_M_Str(o.RemoveHTML(o.GetRegValue(reg, b_List)));
if (ob.Mans == "若干")
{
ob.Mans = "0";
}
else
{
ob.Mans = ob.Mans.Replace("人", "");
}
ob.ValidityDays = "180";
ob.JobType = ddl_JobType.SelectedItem.Text;
ob.JobType = o.Get_JobType(ob.JobType);
if (LocusArea.Length == 1)
{
ob.WorkPlace = com.LocusArea + "-" + com.LocusArea + "|";
}
else
{
ob.WorkPlace = com.LocusArea + "-" + com.LocusCity + "|";
}
reg = "<tdcolspan=6class=txt_4word.+?>.+?</td>";
ob.JobDesc = o.Get_To_Html(o.RemoveHTML(o.Get_Html(o.GetRegValue(reg, s_List))));
ob.ResumeLang = "1";
reg = "薪水范围:+?.+?</td>";
ob.MonthPay = o.Get_M_Str(o.RemoveHTML(o.GetRegValue(reg, b_List)));
if (ob.MonthPay != "")
{
ob.MonthPay = o.Get_Pay(ob.MonthPay);
}
.........
}
这里Do_Data()方法为何没有执行. 但是oThread.IsAlive方法为true.请问题这是怎么回事..??
谢谢! --------------------编程问答-------------------- Thread th = new Thread(new ThreadStart(Do_Data));
th.Start(); --------------------编程问答-------------------- 这样写也不行啊。。。 --------------------编程问答-------------------- 1 先用单线程测试看能运行否
2 //oThread.Abort();
--------------------编程问答-------------------- 你把Do_Data()改得简单一点看是否执行了,
Do_Data()里是否有控制页面控件的代码,如有必须用委托 --------------------编程问答-------------------- 能。。。 --------------------编程问答-------------------- 不用线程的话可以执行。 --------------------编程问答--------------------
这样的啊??里面是读取了页面的控件。。。
我改了。就是打印一个字符。可以用。
--------------------编程问答--------------------
Thread th = new Thread(new ThreadStart(BindData));
th.Start();
--------------------编程问答-------------------- 执行了。这个方法
public void BindData()
{
while (true)
{
WebReference.Message msg = new winform.WebReference.Message();
DataSet ds = msg.GetMsgList();
StringBuilder sb = new StringBuilder();
if (ds != null && ds.Tables.Count != 0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
sb.Append(row["PubDate"].ToString() + " 游客 说\n" + row["MsgContent"].ToString() + "\n");
}
myDelegate n = new myDelegate(GetMsg);
txtMsg.Invoke(n,sb.ToString());
}
Thread.Sleep(2000);
}
}
public delegate void myDelegate(string str);
public void GetMsg(string str)
{
txtMsg.Text += str;
txtMsg.Select(txtMsg.Text.Length, 0);
txtMsg.ScrollToCaret();
}
GetRemoteHtmlCode()
string s = "";
MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
_xmlhttp.open("GET", Url, true, null, null);
_xmlhttp.send("");
if (_xmlhttp.readyState == 4)
{
//s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
s = _xmlhttp.responseText;
}
return s;
这个代码就不行了。 --------------------编程问答-------------------- 上面的线程里我要去更改textbox控件的值
但是如果把修改值的操作直接写在线程里是无法执行的
只能用委托的方法把操作方法放到委托里,线程里去调用该委托 --------------------编程问答-------------------- 你这个是AJAX
还没用到过线程里放AJAX
AJAX没必要放到线程里执行吧 --------------------编程问答-------------------- 这是获取远程页面。。。
我想会不会是这样。。。线程有个执行时间。。一定时间到了,就不执行了。 --------------------编程问答-------------------- 控件部分先不要 直接用线程输出这个string s都不行? --------------------编程问答-------------------- 可能是这个原因,你刚执行star的时候这时执行 Do_Data()
,但是 Do_Data()并没执行完,结果又执行 oThread.Abort();
这样Do_Data()就终止了。可以试试这样做
public bool isrun=false;
protected void btn_Submit_Click(object sender, EventArgs e)
{
Response.Buffer = false;
ThreadStart startDownload = new ThreadStart(Do_Data);
Thread oThread = new Thread(startDownload);
oThread.Name = "线程式";
oThread.Start();
isrun=false;
if(isrun)
{
oThread.Abort();
Response.Write("抓取完毕,线程终止");
Response.End();
}
}
private void Do_Data()
{
//你要执行的内容
isrun=true;
}
补充:.NET技术 , ASP.NET