multi-download 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 {  get set ; }
        
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( null false null );
                DownloadFilesCompleted.Invoke(
this , args);
                
return ;
            }

            Start();

        }
        
       
    }
}

 

转载于:https://www.cnblogs.com/gaotianpu/archive/2010/06/23/1763549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值