• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#的事件用法——实现下载时发生的事件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

c#的事件用法——实现下载时发生的事件

//下载时发出的事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DownloadHandler
{
    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 = 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));

                    }

                }

                if (DownloadEnd != null) //下载结束的事件发生
                {

                    DownloadEnd(this, new DownloadEndEventArgs(url, size));

                }

            }

        }

        private string GetNextUrl()
        {

            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();

    }

    //Main
    class Program
    {
        static void Main(string[] args)
        {
            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 + "%");


        }
    }
}

 

posted @ 2017-03-15 20:26 douzujun 阅读(...) 评论(...) 编辑 收藏

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap