事件示例

  1. using System;
  2.   
  3. public delegate void DownloadStartHandler(object sender, DownloadStartEventArgs e);  //声明委托
  4. public delegate void DownloadEndHandler(object sender, DownloadEndEventArgs e);  
  5. public delegate void DownloadingHandler(object sender, DownloadingEventArgs e);  
  6.   
  7. public class DownloadStartEventArgs
  8. {
  9.     public string Url{ getreturn _url;} set{ _url=value;} }
  10.     private string _url;
  11.   
  12.     public DownloadStartEventArgs( string url ){ this._url = url; }
  13. }
  14.   
  15. public class DownloadEndEventArgs
  16. {
  17.     public string Url{ getreturn _url;} set{ _url=value;} }
  18.     private string _url;
  19.   
  20.     public long ByteCount { get return _byteCount; } set{ _byteCount=value;} }
  21.     private long _byteCount;
  22.   
  23.     public DownloadEndEventArgs( string url, long size  ){ this._url = url; this._byteCount=size; }
  24. }
  25.   
  26. public class DownloadingEventArgs
  27. {
  28.     public string Url{ getreturn _url;} set{ _url=value;} }
  29.     private string _url;
  30.   
  31.     public double Percent { get return _percent; } set{ _percent=value;} }
  32.     private double _percent;
  33.   
  34.     public DownloadingEventArgs( string url, double percent  ){ this._url = url; this._percent=percent; }
  35. }
  36.   
  37.   
  38. public class Crawler
  39. {
  40.   
  41.     public event DownloadStartHandler DownloadStart;  // 声明事件
  42.     public event DownloadEndHandler DownloadEnd;  // 声明事件
  43.     public event DownloadingHandler Downloading;  // 声明事件
  44.   
  45.     public string Name { get{return name;} set{ name=value;} }
  46.     private string name;
  47.     private string site;
  48.   
  49.       
  50.     public Crawler( string name, string site )
  51.     {
  52.         this.name = name;
  53.         this.site = site;
  54.     }
  55.   
  56.   
  57.     public void Craw()
  58.     {   
  59.         whiletrue )
  60.         {
  61.             string url = GetNextUrl();
  62.             if( url == null break;
  63.             long size = GetSizeOfUrl( url );
  64.   
  65.             if( DownloadStart != null //下载开始的事件发生
  66.             {
  67.                 DownloadStart( thisnew DownloadStartEventArgs(url));
  68.             }
  69.               
  70.             forlong i=0; i<size+1024; i+=1024 )
  71.             {
  72.                 //下载数据。。。
  73.                 System.Threading.Thread.Sleep( 100 );
  74.                 double percent = (int)(i*100.0/size);
  75.                 if( percent>100) percent=100;
  76.                   
  77.                 if( Downloading != null //下载数据的事件发生
  78.                 {
  79.                     Downloading( thisnew DownloadingEventArgs(url, percent));
  80.                 }
  81.             }
  82.   
  83.             if( DownloadEnd != null //下载结束的事件发生
  84.             {
  85.                 DownloadEnd( thisnew DownloadEndEventArgs(url, size));
  86.             }
  87.         }
  88.   
  89.     }
  90.     private string GetNextUrl()
  91.     {
  92.         int a = rnd.Next(10);
  93.         if( a == 0 ) return null;
  94.         return site + "/Page" + a + ".htm";
  95.     }
  96.     private long GetSizeOfUrl( string url)
  97.     {
  98.         return rnd.Next(3000 * url.Length);
  99.     }
  100.     private Random rnd = new Random();
  101. }
  102.   
  103. class Test
  104. {
  105.     static void Main()
  106.     {
  107.         Crawler crawler = new Crawler("Crawer101""http://www.pku.edu.cn");
  108.   
  109.         crawler.DownloadStart += new DownloadStartHandler( ShowStart ); //注册事件
  110.         crawler.DownloadEnd += new DownloadEndHandler( ShowEnd );
  111.         crawler.Downloading += new DownloadingHandler( ShowPercent );
  112.           
  113.           
  114.         crawler.Craw();
  115.     }
  116.   
  117.     static void ShowStart(object sender, DownloadStartEventArgs e){
  118.         Console.WriteLine( (sender as Crawler).Name + "开始下载" + e.Url );
  119.     }
  120.     static void ShowEnd(object sender, DownloadEndEventArgs e){
  121.         Console.WriteLine( "\n\r下载" + e.Url + "结束,其下载" + e.ByteCount + "字节" );
  122.     }
  123.     static void ShowPercent(object sender, DownloadingEventArgs e){
  124.         Console.Write( "\r下载" + e.Url + "......." + e.Percent + "%" );
  125.           
  126.     }
  127.   
  128.   
  129. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值