一 C#中的事件
大致上:事件-----回调函数;
二 用户界面中的事件
① 按钮点击事件
② 基本的写法
this.button1.Click+=new System.EventHandler(this.button1_Click);
private void button1_Click(object sender,EventHandler e)
{
this.label1.Text=DateTime.Now.ToString();
}
三 事件
1 事件的声明
public event 委托名 事件名;
2 事件的注册与移除
事件名+= 或 -=
在事件所在类的外面,只能用以上两个运算符
3 事件的发生(激发)
事件名(参数列表)
相当于回调所注册的函数;
实例
网络爬虫
EventWhenDownload.cs
4 定义及使用事件的6步
1) 公用的
① 声明事件参数类:class xxxEventArgs{}
② 声明委托:delegate void xxxEventhandler(obj,args);
3) 在一个类中
① 定义事件:public event类型 名称;
② 发生事件:事件名(参数);
3) 在别的类中
① 定义一个方法:void方法名(obj,args);
② 注册事件:xxx.事件+=new 委托(方法名);
5 事件与委托的关系
事件有点像委托类型的实例
事件一定有相关的委托类型;
与委托实例一样,事件也“包含”多个函数;
事件的运算受更多限制(在类外只能用+=或-=)
事件比委托实例更复杂:
可以定义事件存取器
修饰符event委托类型名 事件名
{
add{e+=value;}
remove{e-=value;}
}
总结:
事件时一种消息机制;
事件源调用事件,别的类注册事件;
事件的类型是一个委托;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 下载时发出事件
{
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; } }
public 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 = GetNexUrl();
if (url == null) break;
long size = GetSizeOfUrl(url);
if (DownloadStart != null)//下载开始的事件发送
{
DownloadStart(this, new DownloadStartEventArgs(url));
}
for (long i = 0; i < size + 1024; i += 1024)
{
//下载数据
System.Threading.Thread.Sleep(100);
double percent = (int)(i * 100.0 / size);
if (percent > 100) percent = 100;
if (Downloading != null)//下载数据的事件发送
{
Downloading(this, new DownloadingEventArgs(url, percent));
}
}
}
}
private string GetNexUrl()
{
int a = rnd.Next(10);
if (a == 0) return null;
return site + "/Page" + a + ".htm";
}
private long GetSizeOfUrl(string url)
{
return rnd.Next(3000 * url.Length);
}
private Random rnd = new Random();
}
class Test
{
static void Main()
{
CrawLer crawler = new CrawLer("Crawer101", "https://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("\n下载" + e.Url + ".........." + e.Percent + "%");
}
}
}