예제 #1
0
        /// <summary>
        /// Start to download.
        /// </summary>
        public void Download()
        {
            // Only idle download client can be started.
            if (this.Status != DownloadStatus.Initialized)
            {
                throw new ApplicationException(
                          "Only Initialized download client can be started.");
            }

            this.EnsurePropertyValid();

            // Set the status.
            this.Status = DownloadStatus.Downloading;

            if (!this.HasChecked)
            {
                string filename = null;
                this.CheckUrlAndFile(out filename);
            }

            HttpDownloadClient client = new HttpDownloadClient(
                this.Url.AbsoluteUri,
                0,
                long.MaxValue,
                this.BufferSize,
                this.BufferCountPerNotification * this.BufferSize,
                this.BufferCountPerNotification);

            // Set the HasChecked flag, so the client will not check the URL again.
            client.TotalSize    = this.TotalSize;
            client.DownloadPath = this.DownloadPath;
            client.HasChecked   = true;
            client.Credentials  = this.Credentials;
            client.Proxy        = this.Proxy;

            // Register the events of HttpDownloadClients.
            client.DownloadProgressChanged += client_DownloadProgressChanged;
            client.StatusChanged           += client_StatusChanged;
            client.DownloadCompleted       += client_DownloadCompleted;

            this.downloadClients.Add(client);
            client.Download();
        }
        /// <summary>
        /// 开始下载。
        /// </summary>
        public void Start()
        {
            // 检查文件是否存在。
            CheckFileOrCreateFile();

            // 只有状态是Checked 时,才会开始下载 。
            if (this.status != MultiThreadedWebDownloaderStatus.Checked)
            {
                throw new ApplicationException(
                    "只有状态是Checked 时,才会开始下载 。");
            }

            // 如果文件不支持"Accept-Ranges",然后创建一个 HttpDownloadClient
            // 使用单线程下载文件。 否则就创建多个HttpDownloadClients下载文件。

            if (!IsRangeSupported)
            {
                HttpDownloadClient client = new HttpDownloadClient(
                    this.Url,
                    this.DownloadPath,
                    0,
                    long.MaxValue,
                    this.BufferSize,
                    this.BufferCountPerNotification);
                client.TotalSize = this.TotalSize;
                this.downloadClients.Add(client);
            }
            else
            {
                // 计算每个client要下载的大小。
                int maxSizePerThread =
                    (int)Math.Ceiling((double)this.TotalSize / this.MaxThreadCount);
                if (maxSizePerThread < this.MaxCacheSize)
                {
                    maxSizePerThread = this.MaxCacheSize;
                }

                long leftSizeToDownload = this.TotalSize;

                // 线程的个数为:  MaxThreadCount 的最小值+ TotalSize / MaxCacheSize.

                int threadsCount =
                    (int)Math.Ceiling((double)this.TotalSize / maxSizePerThread);

                for (int i = 0; i < threadsCount; i++)
                {
                    long endPoint = maxSizePerThread * (i + 1) - 1;
                    long sizeToDownload = maxSizePerThread;

                    if (endPoint > this.TotalSize)
                    {
                        endPoint = this.TotalSize - 1;
                        sizeToDownload = endPoint - maxSizePerThread * i;
                    }

                    // 下载整个文件的一部分。
                    HttpDownloadClient client = new HttpDownloadClient(
                        this.Url,
                        this.DownloadPath,
                        maxSizePerThread * i,
                        endPoint);

                    client.TotalSize = sizeToDownload;
                    this.downloadClients.Add(client);
                }
            }

            // 设置 lastStartTime ,用于计算用时多少。
            lastStartTime = DateTime.Now;

            // 设置状态为:downloading
            this.Status = MultiThreadedWebDownloaderStatus.Downloading;

            //开始所有的HttpDownloadClients.
            foreach (var client in this.downloadClients)
            {
                if (this.Proxy != null)
                {
                    client.Proxy = this.Proxy;
                }

                //加载 HttpDownloadClients事件。
                client.DownloadProgressChanged +=
                    new EventHandler<HttpDownloadClientProgressChangedEventArgs>(
                        client_DownloadProgressChanged);

                client.StatusChanged += new EventHandler(client_StatusChanged);

                client.ErrorOccurred += new EventHandler<ErrorEventArgs>(
                    client_ErrorOccurred);
                client.Start();
            }
        }
        /// <summary>
        /// 开始下载。
        /// </summary>
        public void Start()
        {
            // 检查文件是否存在。
            CheckFileOrCreateFile();

            // 只有状态是Checked 时,才会开始下载 。
            if (this.status != MultiThreadedWebDownloaderStatus.Checked)
            {
                throw new ApplicationException(
                          "只有状态是Checked 时,才会开始下载 。");
            }

            // 如果文件不支持"Accept-Ranges",然后创建一个 HttpDownloadClient
            // 使用单线程下载文件。 否则就创建多个HttpDownloadClients下载文件。

            if (!IsRangeSupported)
            {
                HttpDownloadClient client = new HttpDownloadClient(
                    this.Url,
                    this.DownloadPath,
                    0,
                    long.MaxValue,
                    this.BufferSize,
                    this.BufferCountPerNotification);
                client.TotalSize = this.TotalSize;
                this.downloadClients.Add(client);
            }
            else
            {
                // 计算每个client要下载的大小。
                int maxSizePerThread =
                    (int)Math.Ceiling((double)this.TotalSize / this.MaxThreadCount);
                if (maxSizePerThread < this.MaxCacheSize)
                {
                    maxSizePerThread = this.MaxCacheSize;
                }

                long leftSizeToDownload = this.TotalSize;

                // 线程的个数为:  MaxThreadCount 的最小值+ TotalSize / MaxCacheSize.

                int threadsCount =
                    (int)Math.Ceiling((double)this.TotalSize / maxSizePerThread);

                for (int i = 0; i < threadsCount; i++)
                {
                    long endPoint       = maxSizePerThread * (i + 1) - 1;
                    long sizeToDownload = maxSizePerThread;

                    if (endPoint > this.TotalSize)
                    {
                        endPoint       = this.TotalSize - 1;
                        sizeToDownload = endPoint - maxSizePerThread * i;
                    }

                    // 下载整个文件的一部分。
                    HttpDownloadClient client = new HttpDownloadClient(
                        this.Url,
                        this.DownloadPath,
                        maxSizePerThread * i,
                        endPoint);

                    client.TotalSize = sizeToDownload;
                    this.downloadClients.Add(client);
                }
            }

            // 设置 lastStartTime ,用于计算用时多少。
            lastStartTime = DateTime.Now;

            // 设置状态为:downloading
            this.Status = MultiThreadedWebDownloaderStatus.Downloading;

            //开始所有的HttpDownloadClients.
            foreach (var client in this.downloadClients)
            {
                if (this.Proxy != null)
                {
                    client.Proxy = this.Proxy;
                }

                //加载 HttpDownloadClients事件。
                client.DownloadProgressChanged +=
                    new EventHandler <HttpDownloadClientProgressChangedEventArgs>(
                        client_DownloadProgressChanged);

                client.StatusChanged += new EventHandler(client_StatusChanged);

                client.ErrorOccurred += new EventHandler <ErrorEventArgs>(
                    client_ErrorOccurred);
                client.Start();
            }
        }
예제 #4
0
        void DownloadInternal(object obj)
        {
            if (this.Status != DownloadStatus.Waiting)
            {
                return;
            }

            try
            {
                this.EnsurePropertyValid();

                // Set the status.
                this.Status = DownloadStatus.Downloading;

                if (!this.HasChecked)
                {
                    string filename = null;
                    this.CheckUrlAndFile(out filename);
                }



                // If the file does not support "Accept-Ranges" header, then create one
                // HttpDownloadClient to download the file in a single thread, else create
                // multiple HttpDownloadClients to download the file.
                if (!IsRangeSupported)
                {
                    HttpDownloadClient client = new HttpDownloadClient(
                        this.Url.AbsoluteUri,
                        0,
                        long.MaxValue,
                        this.BufferSize,
                        this.BufferCountPerNotification * this.BufferSize,
                        this.BufferCountPerNotification);

                    // Set the HasChecked flag, so the client will not check the URL again.
                    client.TotalSize    = this.TotalSize;
                    client.DownloadPath = this.DownloadPath;
                    client.HasChecked   = true;
                    client.Credentials  = this.Credentials;
                    client.Proxy        = this.Proxy;

                    this.downloadClients.Add(client);
                }
                else
                {
                    // Calculate the block size for each client to download.
                    int maxSizePerThread =
                        (int)Math.Ceiling((double)this.TotalSize / this.MaxThreadCount);
                    if (maxSizePerThread < this.MaxCacheSize)
                    {
                        maxSizePerThread = this.MaxCacheSize;
                    }

                    long leftSizeToDownload = this.TotalSize;

                    // The real threads count number is the min value of MaxThreadCount and
                    // TotalSize / MaxCacheSize.
                    int threadsCount =
                        (int)Math.Ceiling((double)this.TotalSize / maxSizePerThread);

                    for (int i = 0; i < threadsCount; i++)
                    {
                        long endPoint       = maxSizePerThread * (i + 1) - 1;
                        long sizeToDownload = maxSizePerThread;

                        if (endPoint > this.TotalSize)
                        {
                            endPoint       = this.TotalSize - 1;
                            sizeToDownload = endPoint - maxSizePerThread * i;
                        }

                        // Download a block of the whole file.
                        HttpDownloadClient client = new HttpDownloadClient(
                            this.Url.AbsoluteUri,
                            maxSizePerThread * i,
                            endPoint);

                        // Set the HasChecked flag, so the client will not check the URL again.
                        client.DownloadPath = this.DownloadPath;
                        client.HasChecked   = true;
                        client.TotalSize    = sizeToDownload;
                        client.Credentials  = this.Credentials;
                        client.Proxy        = this.Proxy;
                        this.downloadClients.Add(client);
                    }
                }

                // Set the lastStartTime to calculate the used time.
                lastStartTime = DateTime.Now;

                // Start all HttpDownloadClients.
                foreach (var client in this.downloadClients)
                {
                    if (this.Proxy != null)
                    {
                        client.Proxy = this.Proxy;
                    }

                    // Register the events of HttpDownloadClients.
                    client.DownloadProgressChanged += client_DownloadProgressChanged;
                    client.StatusChanged           += client_StatusChanged;
                    client.DownloadCompleted       += client_DownloadCompleted;


                    client.BeginDownload();
                }
            }
            catch (Exception ex)
            {
                this.Cancel();
                this.OnDownloadCompleted(new DownloadCompletedEventArgs(
                                             null,
                                             this.DownloadedSize,
                                             this.TotalSize,
                                             this.TotalUsedTime,
                                             ex));
            }
        }