首页 新闻 搜索 专区 学院

加载数据慢问题,多线程问题,急请高手帮忙。。。

0
悬赏园豆:10 [待解决问题]

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

namespace Factory
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        //创建一个DataTable对象,用来保存商家的信息
        DataTable dt= new DataTable();

        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;
        }

      


        //点击按钮显示信息
        private void btnShow_Click(object sender, EventArgs e)
        {
            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 <= maxPage; i++)
            //{
            //    GetDataByUrl(webUrl + "p" + i);
            //}
            GetDataByUrl(webUrl);
            // 把获取的结果绑定到dataGridView上
            this.dgvDetails.DataSource = dt;

          
        }
       
        private void GetDataByUrl(string url)
        {
            // 得到指定Url的源码
            string strWebContent = GetWebContent(url);
            for (int i = 0; i < 15; i++) // 每页显示15条好像是固定的
            {
                string name = GetSubString(ref strWebContent, ");\" target=\"_blank\" >", "</a>");
                string addr = GetSubString(ref strWebContent, "\" class=\"Black-H\">", "&nbsp;&nbsp;").Replace("</a>", "");
                string tel = GetSubString(ref strWebContent, "", "</li>");
                //获取经纬度
                string latlng = GetlatlngByAddress(addr);
             
                dt.Rows.Add(name, addr, tel,latlng);
               
            }

        }


        // 截取字符串
        private string GetSubString(ref string content, string beginStr, string endStr)
        {
            //开始位置
            int begin = content.IndexOf(beginStr) + beginStr.Length;
            //结束位置
            int end = content.IndexOf(endStr, begin);
            if (begin == -1 || end == -1 || begin > end)
                return "索引出错。。。";
            string result = content.Substring(begin, end - begin);
            content = content.Remove(0, end + endStr.Length);
            return result;
        }

        //把获取的商家信息保存到数据库中
        private void btnSave_Click(object sender, EventArgs e)
        {
            BusDAL dal = new BusDAL();
            if (dal.InsertBusinesser(dt) >= 1)
            {
                MessageBox.Show("插入成功!", "提示");
            }
            else
            {
                MessageBox.Show("插入失败!", "提示");
            }
        }

        /// <summary>  
        /// 根据地址获取经度和纬度  
        /// </summary>

        public string GetlatlngByAddress(string address)
        {
            string result = string.Empty;
            try
            {

              
                //转换字符编码
                address = HttpUtility.UrlEncode(address, Encoding.UTF8);
                result = GetWebContent("http://maps.google.com/maps/geo?q=" + address + "&output=xml&sensor=true&key=dc27b1d958ee7725aa1b6899af7b50816258da9bf3ffa0f736db1bf3ca24877b7a25104e7f587e1c");
                //解析所返回的带命名空间的xml并返回其中指定路径下的一个节点值
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(result);
                XmlNamespaceManager xmlns = new XmlNamespaceManager(xml.NameTable);
                xmlns.AddNamespace("e", "http://earth.google.com/kml/2.0");
                result = xml.SelectSingleNode("e:kml/e:Response/e:Placemark/e:Point/e:coordinates", xmlns).InnerText;
            }
            catch
            {
                result = null;
            }
            return result;

          

           
        }
      

    }
 
}


 

KittyYan的主页 KittyYan | 初学一级 | 园豆:190
提问于:2011-10-17 10:40
< >
分享
所有回答(4)
0

这个提高速度可能要考虑很多方面的,1,用多线程进行页面的加载,用后台线程同时访问多个页面进行数据处理。2,提高网速,3,用正则表达式解析html

LCM | 园豆:6876 (大侠五级) | 2011-10-17 11:24

恩,就是加载的时候用多线程,但是我不会用多线程,后台线程同时访问多个页面进行数据处理也不会,我用了多线程老是出错。。。

支持(0) 反对(0) KittyYan | 园豆:190 (初学一级) | 2011-10-17 13:39
0

我们的程序员们真忙啊,贴一大堆代码,没几个字描述具体要解决的问题、自己的分析、尝试过的解决方法。。。

bye_passer | 园豆:367 (菜鸟二级) | 2011-10-17 11:26
0

一看2楼就是个幽默的人 关注一下

zakkia | 园豆:280 (菜鸟二级) | 2011-10-17 11:53
0

你可以尝试应用缓存

abner.Y | 园豆:211 (菜鸟二级) | 2011-10-17 22:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册