using System;

public delegate void DownloadStartHandler(object sender, DownloadStartEventArgs e);  //声明委托
public delegate void DownloadEndHandler(object sender, DownloadEndEventArgs e);
public delegate void DownloadingHandler(object sender, DownloadingEventArgs e); public class DownloadStartEventArgs
{
public string Url{ get{ return _url;} set{ _url=value;} }
private string _url; public DownloadStartEventArgs( string url ){ this._url = url; }
} public class DownloadEndEventArgs
{
public string Url{ get{ return _url;} set{ _url=value;} }
private string _url; public long ByteCount { get { return _byteCount; } set{ _byteCount=value;} }
private long _byteCount; public DownloadEndEventArgs( string url, long size ){ this._url = url; this._byteCount=size; }
} public class DownloadingEventArgs
{
public string Url{ get{ return _url;} set{ _url=value;} }
private string _url; public double Percent { get { return _percent; } set{ _percent=value;} }
private double _percent; public DownloadingEventArgs( string url, double percent ){ this._url = url; this._percent=percent; }
} public class Crawler
{ public event DownloadStartHandler DownloadStart; // 声明事件
public event DownloadEndHandler DownloadEnd; // 声明事件
public event DownloadingHandler Downloading; // 声明事件 public string Name { get{return name;} set{ name=value;} }
private string name;
private string site; public Crawler( string name, string site )
{
this.name = name;
this.site = site;
} public void Craw()
{
while( true )
{
string url = GetNextUrl();
if( url == null ) break;
long size = GetSizeOfUrl( url ); if( DownloadStart != null ) //下载开始的事件发生
{
DownloadStart( this, new DownloadStartEventArgs(url));
} for( long i=; i<size+; i+= )
{
//下载数据。。。
System.Threading.Thread.Sleep( );
double percent = (int)(i*100.0/size);
if( percent>) percent=; if( Downloading != null ) //下载数据的事件发生
{
Downloading( this, new DownloadingEventArgs(url, percent));
}
} if( DownloadEnd != null ) //下载结束的事件发生
{
DownloadEnd( this, new DownloadEndEventArgs(url, size));
}
} }
private string GetNextUrl()
{
int a = rnd.Next();
if( a == ) return null;
return site + "/Page" + a + ".htm";
}
private long GetSizeOfUrl( string url)
{
return rnd.Next( * url.Length);
}
private Random rnd = new Random();
} class Test
{
static void Main()
{
Crawler crawler = new Crawler("Crawer101", "http://www.pku.edu.cn"); crawler.DownloadStart += new DownloadStartHandler( ShowStart ); //注册事件
crawler.DownloadEnd += new DownloadEndHandler( ShowEnd );
crawler.Downloading += new DownloadingHandler( ShowPercent ); crawler.Craw();
} static void ShowStart(object sender, DownloadStartEventArgs e){
Console.WriteLine( (sender as Crawler).Name + "开始下载" + e.Url );
}
static void ShowEnd(object sender, DownloadEndEventArgs e){
Console.WriteLine( "\n\r下载" + e.Url + "结束,其下载" + e.ByteCount + "字节" );
}
static void ShowPercent(object sender, DownloadingEventArgs e){
Console.Write( "\r下载" + e.Url + "......." + e.Percent + "%" ); } }

4.2 event的更多相关文章

  1. 如何利用ETW(Event Tracing for Windows)记录日志

    ETW是Event Tracing for Windows的简称,它是Windows提供的原生的事件跟踪日志系统.由于采用内核(Kernel)层面的缓冲和日志记录机制,所以ETW提供了一种非常高效的事 ...

  2. [.NET] C# 知识回顾 - Event 事件

    C# 知识回顾 - Event 事件 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6060297.html 序 昨天,通过<C# 知识回顾 - ...

  3. Atitit 解决Unhandled event loop exception错误的办法

    Atitit 解决Unhandled event loop exception错误的办法 查看workspace/.metadata/.log org.eclipse.swt.SWTError: No ...

  4. Java模拟Windows的Event

    场景 开发中遇到一个场景,业务操作会不定时的产生工作任务,这些工作任务需要放入到一个队列中,而另外会有一个线程一直检测这个队列,队列中有任务就从队列中取出并进行运算. 问题 业务场景倒是简单,只不过这 ...

  5. 事件EVENT与waitforsingleobject的使用

    事件event与waitforsingleobject的配合使用,能够解决很多同步问题,也可以在数据达到某个状态时启动另一个线程的执行,如报警. event的几个函数: 1.CreateEvent和O ...

  6. 火狐浏览器中event不起作用解决办法--记录(一)

    今天遇到了这个问题.IE,谷歌下都没问题,但在FF下却不起作用,很郁闷查了半天,看别人博文写了老长,结果试了要么起作用,但太麻烦,要么不起作用,说了那么多跟没说一样. 其实只要这一句代码就行:e=ar ...

  7. Event事件

    妙味课堂-Event事件 1.焦点:当一个元素有焦点的时候,那么他就可以接受用户的输入(不是所有元素都能接受焦点) 给元素设置焦点的方式: 1.点击 2.tab 3.js 2.(例子:输入框提示文字) ...

  8. Event Sourcing Pattern 事件源模式

    Use an append-only store to record the full series of events that describe actions taken on data in ...

  9. 严重: Exception sending context initialized event to listener instance of class

    问题描述:Exception sending context initialized event to listener instance of class org.springframework.w ...

  10. [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)

    原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...

随机推荐

  1. keepalived+lvs高可用集群

    LVS+Keepalived 介绍 LVS LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国 ...

  2. C#中值和引用

    c#中有两种基本类型,它们分别是值类型和引用类型:而每种类型都可以细分为如下类型: ps:1.基本类型是值类型 2.类.接口.委托都是引用类型

  3. Python3基础 help 查看内置函数说明

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. POJ 2752 Seek the Name, Seek the Fame(KMP中next的理解)题解

    题意: 要求你给出每个前后缀相同的串的长度,比如: "alala"的前缀分别为{"a", "al", "ala", &q ...

  5. 【安装】Mysql在Linux上安装

    1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:mysql-5.6.37-linux-glibc2.12- ...

  6. c#进阶之Delegate

    委托是什么?答:委托是一种类型   等同与 一个class类,继承System.MulticastDelegate,但mult....gate是一个特殊类,不能够派生 委托的调用,如何去使用 1/委托 ...

  7. DataTables warning: table id=data-table - Requested unknown parameter '3' for row 0.

    本文为博主原创,未经允许,不得转载: 在使用jquery 的datatable时,报错在页面弹出弹出框,并提示以下内容: DataTables warning: table id=data-table ...

  8. The way to Go(5): 文件名、关键字与标识符

    Reference: Github: Go Github: The way to Go 文件名 1.后缀名:.go 2.小写字母 3._分割名称 4.不包含空格或其他特殊字符 标识符 有效的标识符必须 ...

  9. HDU 5887 Herbs Gathering(搜索求01背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做, ...

  10. linux 系统管理的10个小技巧

    1.恢复屏幕 尝试输入:#cat /bin/cat 输入的屏幕内容非常凌乱,那么该怎么做? 输入:#reset 那么屏幕恢复正常了,比关闭再次登录好多了,特别是经过至少5台机器和SSH2才能到达 2. ...