日期:2014-05-20  浏览次数:20730 次

多线程问题
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()