小弟正在做一个discuz群发机,在本地搭建的环境,想一次发布多个贴子。我的意思是想创建N条线程,然后每个线程都是独立进行POST,那样就不会因为POST执行太快而失败了。
但我发现我写的代码虽然是创建了N条线程,但执行起来就好像只创建了一条线程一样。
请问怎么实现每个线程都是独立进行POST,而不会像单线程那样执行完一次POST 又要等一会才执行下一次POST
代码:
[C#] 纯文本查看 复制代码 public delegate void UpdateTxtdelegate();
UpdateTxtdelegate updateTxtdelegatelg;
UpdateTxtdelegate updateTxtdelegategd;
UpdateTxtdelegate updateTxtdelegatebd;
private void update()
{
richStatus.AppendText("账号:" + txtUser.Text + "登陆成功" + "\n");
richStatus.ScrollToCaret();
}
private void updategood()
{
richStatus.AppendText("发布成功" + "\n");
}
private void updatebad()
{
richStatus.AppendText("发布失败"+"\n");
}
//登陆
private void btnLogin_Click(object sender, EventArgs e)
{
item.Method = "POST";
item.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/msword, */";
item.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0)";
item.ContentType = "application/x-www-form-urlencoded";
item.URL = "http://localhost/bbs/" + "/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1";
item.Postdata = "fastloginfield=username&username="+"cesi"+"&password="+"123456"+"&quickforward=yes&handlekey=ls";
result = tool.GetHtml(item);
foreach (CookieItem ck in HttpCookieHelper.GetCookieList(result.Cookie))
{
if (ck.Key.Contains("4F1R_"))
{
cook += HttpCookieHelper.CookieFormat(ck.Key, ck.Value);
}
}
item = new HttpItem();
item.Method = "GET";
item.Cookie = cook;
item.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/msword, */";
item.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0)";
item.ContentType = "application/x-www-form-urlencoded";
item.URL = "http://localhost/bbs/member.php?mod=logging&action=login";
result = tool.GetHtml(item);
formhash = Regex.Match(result.Html, @"(?<=formhash=)\w+").Value;
if (cook.Contains("4F1R"))
{
//登陆成功
richStatus.Invoke(updateTxtdelegatelg);
}
}
//发布
private void btnPabu_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Thread tre = new Thread(Fabu);
tre.Start();
}
}
private void Fabu()
{
item = new HttpItem();
item.Cookie = cook;
item.Allowautoredirect = true;
item.Method = "POST";
item.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/msword, */";
item.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0)";
item.ContentType = "application/x-www-form-urlencoded";
item.URL = "http://localhost/bbs/forum.php?mod=post&action=newthread&fid=2&extra=&topicsubmit=yes";
item.Postdata = "formhash=" + formhash + "&posttime=1387595124&wysiwyg=1&subject=" + "测试文章标题" + "&message=" + "测试文章内容啊啊啊吼叫啊" + "&replycredit_extcredits=0&replycredit_times=1&replycredit_membertimes=1&replycredit_random=100&readperm=&price=&tags=&rushreplyfrom=&rushreplyto=&rewardfloor=&replylimit=&stopfloor=&creditlimit=&allownoticeauthor=1&usesig=1&save=";
result = tool.GetHtml(item);
//richStatus.AppendText("正在发贴......"+"\n");
if (result.Html.Contains("测试"))
{
//发布成功
richStatus.Invoke(updateTxtdelegategd);
}
else
{
//发布失败
richStatus.Invoke(updateTxtdelegatebd);
}
////既然是多线程,还需要Thread.Sleep吗????小弟不解///
Thread.Sleep(1000);
}
|