异步操作的一个例子

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using System.Text;
  9. using System.Net;
  10. using System.Threading;
  11. using System.Data;
  12. using System.Collections;
  13. namespace getPageValue
  14. {
  15.     public partial class Four : System.Web.UI.Page
  16.     {
  17.         private delegate string DelegateName(string strIP, out string OldIP);
  18.         public DBClass db = new DBClass();
  19.         protected void Page_Load(object sender, EventArgs e)
  20.         {
  21.             if (!Page.IsPostBack)
  22.             {
  23.                 DelegateName dn = new DelegateName(MethodName);
  24.                 string strsql = "select top 30 * from UserCode where UserIP<>'' and (Code is null or code = '')";
  25.                 DataTable dt = db.GetDataTable(strsql);
  26.                 for (int i = 0; i < dt.Rows.Count; i++)
  27.                 {
  28.                     AsyncCallback acb = new AsyncCallback(CallBackMethod);
  29.                     string OldIP;
  30.                     IAsyncResult iar = dn.BeginInvoke(dt.Rows[i]["id"].ToString(), out OldIP, acb, dn);
  31.                     //每次调用间隔一秒,通过实验得到,如果你的数据量大,最好间隔5秒
  32.                     iar.AsyncWaitHandle.WaitOne();
  33.                     Thread.Sleep(5000);
  34.                 }
  35.                 dt.Dispose();
  36.             }
  37.         }
  38.         private void CallBackMethod(IAsyncResult ar)
  39.         {
  40.             //从异步状态ar.AsyncState中,获取委托对象
  41.             DelegateName dn = (DelegateName)ar.AsyncState;
  42.             //输出参数
  43.             string OldIP;
  44.             //一定要EndInvoke,否则你的下场很惨
  45.             string r = dn.EndInvoke(out  OldIP, ar);
  46.             //MessageBox.Show("异步完成喽!i的值是" + i.ToString() + ",r的值是" + r);
  47.             string strsql = "update UserCode set Code='" + r + "' where id=" + OldIP + "";
  48.             try
  49.             {
  50.                 db.ExecuteSql(strsql);
  51.             }
  52.             catch
  53.             {
  54.             }
  55.             
  56.         }
  57.         private string MethodName(string strID, out string OldIP)
  58.         {
  59.             //定义变量
  60.             //singleid 传入的用户信息ID
  61.             //singleip 根据用户信息ID,而取到的IP
  62.             //strAction post必传值之一
  63.             //strCode 传入用户信息表中的code值
  64.             //
  65.             OldIP = strID;
  66.             string singleid = strID;
  67.             string singleip = db.GetSingleValue("UserCode""UserIP""id=" + singleid).Trim();
  68.             string strAction = "2";
  69.             string strCode = "";
  70.             ASCIIEncoding encoding = new ASCIIEncoding();
  71.             string postData = "ip=" + singleip;
  72.             postData += "&action=" + strAction;
  73.             byte[] data = encoding.GetBytes(postData);
  74.             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www2.ip138.com/ips8.asp");
  75.             myRequest.Method = "POST";
  76.             myRequest.ContentType = "application/x-www-form-urlencoded";
  77.             myRequest.ContentLength = data.Length;
  78.             Stream myStream = myRequest.GetRequestStream();
  79.             myStream.Write(data, 0, data.Length);
  80.             myStream.Close();
  81.             HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  82.             StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
  83.             string content = reader.ReadToEnd();
  84.             //判断是否是有省市的信息,如果信息中有省,市,则进行存入数据库,如为127.0.0.1,是直接给本省code。其它信息,则不给记录
  85.             if (singleip == "127.0.0.1")
  86.             {
  87.                 strCode = "0008";
  88.             }
  89.             else
  90.             {
  91.                 if (content.IndexOf("省") == -1 && content.IndexOf("市") == -1 && singleid != "127.0.0.1")
  92.                 {
  93.                     if (content.IndexOf("查询太频繁") != -1)
  94.                     {
  95.                         strCode = "查询太频繁";
  96.                     }
  97.                     else
  98.                     {
  99.                         strCode = "0035";
  100.                     }
  101.                 }
  102.                 else
  103.                 {
  104.                     if (content.IndexOf("省") != -1)
  105.                     {
  106.                         string con = content.Substring(content.IndexOf("本站主数据") + 6, content.IndexOf("</li><li>参考数据一") - content.IndexOf("本站主数据") - 1);
  107.                         string strpro = con.Substring(0, con.IndexOf("省") + 1);
  108.                         strCode = db.GetSingleValue("S_Province""ProvinceCode""ProvinceName='" + strpro + "'").Trim();
  109.                         //strCode = strpro;
  110.                     }
  111.                     if (content.IndexOf("市") != -1)
  112.                     {
  113.                         string con = content.Substring(content.IndexOf("本站主数据") + 6, content.IndexOf("</li><li>参考数据一") - content.IndexOf("本站主数据") - 1);
  114.                         string strpro = con.Substring(con.IndexOf("省") + 1, con.IndexOf("市") - con.IndexOf("省"));
  115.                         strCode += db.GetSingleValue("S_City""ZipCode""CityName='" + strpro + "'").Trim(); ;
  116.                     }
  117.                 }
  118.             }
  119.             if (strCode == "")
  120.             {
  121.                 strCode = "0035";
  122.             }
  123.             //strIP = 
  124.             return strCode;
  125.         }
  126.     }
  127. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值