• multidownload files 多线程下载


    using System;
    using System.Net;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;

    namespace Downloader
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                List
    <string> uriList = new List<string>();

                
    #region init data
                uriList.Add(
    "http://t1.gstatic.com/images?q=tbn:lJZx9zTCrSd9hM:");
                
    //add more here
                
    #endregion

                
    //multi-download
                Stopwatch sw = new Stopwatch();
                sw.Start();
                DownloaderOlder oldMDownloader 
    = new DownloaderOlder();
                oldMDownloader.DownloadFilesCompleted 
    += delegate
                {
                    sw.Stop();
                    Console.WriteLine(
    "ok:" + sw.ElapsedMilliseconds);
                    Console.ReadLine();
                };
                oldMDownloader.Download(uriList);

                
    //single-download
                Stopwatch sw1 = new Stopwatch();
                sw1.Start();
                oldMDownloader.DownloadSingle(uriList);
                sw1.Stop();
                Console.WriteLine(
    "ok2:{0},{1}", sw1.ElapsedMilliseconds, sw1.ElapsedMilliseconds / sw.ElapsedMilliseconds);

                
    //multi-download client can set the max-thread count
                Stopwatch sw2 = new Stopwatch();
                sw2.Start();
                MultiDownloader mdownloader 
    = new MultiDownloader(uriList);
                mdownloader.MaxThreadCount 
    = 100;
                mdownloader.DownloadFilesCompleted 
    += delegate
                {
                    sw2.Stop();
                    Console.WriteLine(
    "ok3:{0}", sw2.ElapsedMilliseconds);
                    Console.ReadLine();
                };
                mdownloader.Start();

                Console.ReadLine();
            }
        }

        
    public class DownloaderOlder
        {
            
    public event AsyncCompletedEventHandler DownloadFilesCompleted;

            
    private int TotalCount = 0;
            
    private int count = 0;
            
    public void Download(List<string> uriList)
            {
                TotalCount 
    = uriList.Count;

                
    foreach (var uri in uriList)
                {
                    Download(uri);
                }
            }

            
    public void DownloadSingle(List<string> uriList)
            {
                
    foreach (var uri in uriList)
                {
                    
    string file = string.Format(@"d:\asy{0}.jpg",uri.GetHashCode());

                    WebClient client 
    = new WebClient();
                    client.DownloadFile(uri, file);

                }
            }

            
    public void Download(string uri)
            {
                
    string file = string.Format(@"d:\{0}.jpg",uri.GetHashCode());
                
                WebClient client 
    = new WebClient();
                client.DownloadFileCompleted 
    += new AsyncCompletedEventHandler(onCompleted);
                client.DownloadFileAsync(
    new Uri(uri), file);
                
            }

            
    public void onCompleted(object sender,AsyncCompletedEventArgs e)
            {           
                count
    ++;
                
    if (count == TotalCount)
                {
                    AsyncCompletedEventArgs args 
    =new AsyncCompletedEventArgs(null,false,null);
                    DownloadFilesCompleted.Invoke(
    this, args);
                }
                
            }

            
        }

        
    public class MultiDownloader
        {
            
    public event AsyncCompletedEventHandler DownloadFilesCompleted;
            
    public int MaxThreadCount { getset; }
            
    private int _CurrentThreadCount = 0;

            
    private int _TotalCount = 0;
            
    private int _DownloadedCount = 0;
            
    private Queue<Uri> _DownloadQueue = new Queue<Uri>();

            
    public bool CanStartThread
            {
                
    get 
                {
                    
    if (_DownloadQueue.Count == 0)
                        
    return false;

                    
    if (_CurrentThreadCount == MaxThreadCount)
                        
    return false;

                    
    return true;
                }
            }

            
    public bool DownloadCompleted
            {
                
    get { return _DownloadedCount == _TotalCount; }
            }
            

            
    public MultiDownloader(List<string> uriList)
            {
                MaxThreadCount 
    = 5;
                _TotalCount 
    = uriList.Count;

                
    foreach (var strURI in uriList)
                    _DownloadQueue.Enqueue(
    new Uri(strURI));
            }

            
    public void Start()
            {
                
    while (CanStartThread)
                {
                    Uri uri 
    = null;
                    
    lock (_DownloadQueue)
                    {
                        uri 
    = _DownloadQueue.Dequeue();
                    }

                    
    if (uri == null)
                        
    continue;

                    
    string file = string.Format(@"d:\{0}.jpg", uri.GetHashCode());

                    WebClient client 
    = new WebClient();
                    client.DownloadFileCompleted 
    += new AsyncCompletedEventHandler(onCompleted);
                    client.DownloadFileAsync(uri, file);
                }
            }

            
    public void onCompleted(object sender, AsyncCompletedEventArgs e)
            {
                _DownloadedCount
    ++;

                
    if (DownloadCompleted)
                {
                    AsyncCompletedEventArgs args 
    = new AsyncCompletedEventArgs(nullfalsenull);
                    DownloadFilesCompleted.Invoke(
    this, args);
                    
    return;
                }

                Start();

            }
            
           
        }
    }
  • 相关阅读:
    云时代架构阅读笔记十一——分布式架构中数据一致性常见的几个问题
    云时代架构阅读笔记十——支付宝架构师眼中的高并发架构
    云时代架构阅读笔记九——Disruptor无锁框架为啥这么快
    云时代架构阅读笔记八——JVM性能调优
    lightoj 1024 (高精度乘单精度)
    lightoj 1023
    lightoj 1022
    codeforces 260 div2 C题
    codeforces 260 div2 B题
    codedorces 260 div2 A题
  • 原文地址:https://www.cnblogs.com/gaotianpu/p/1763549.html
Copyright © 2020-2023  润新知