Пример #1
0
        /// <summary>
        /// Start to download.
        /// </summary>
        public void Start()
        {
            // Check whether the destination file exist.
            CheckFileOrCreateFile();

            // Only Checked downloader can be started.
            if (this.status != MultiThreadedWebDownloaderStatus.Checked)
            {
                throw new ApplicationException(
                          "Only Checked downloader can be started. ");
            }

            // 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,
                    this.DownloadPath,
                    0,
                    long.MaxValue,
                    this.BufferSize,
                    this.BufferCountPerNotification * this.BufferSize);
                client.TotalSize = this.TotalSize;
                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,
                        this.DownloadPath,
                        maxSizePerThread * i,
                        endPoint);

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

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

            // Set the downloading status.
            this.Status = MultiThreadedWebDownloaderStatus.Downloading;

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

                // Register the events of HttpDownloadClients.
                client.DownloadProgressChanged +=
                    new EventHandler <HttpDownloadClientProgressChangedEventArgs>(
                        client_DownloadProgressChanged);

                client.StatusChanged += new EventHandler(client_StatusChanged);

                client.ErrorOccurred += new EventHandler <ErrorEventArgs>(
                    client_ErrorOccurred);
                client.Start();
            }
        }
        /// <summary>
        /// Start to download.
        /// </summary>
        public void Start()
        {
            // Check whether the destination file exist.
            CheckFileOrCreateFile();

            // Only Checked downloader can be started.
            if (this.status != MultiThreadedWebDownloaderStatus.Checked)
            {
                throw new ApplicationException(
                    "Only Checked downloader can be started. ");
            }

            // 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,
                    this.DownloadPath,
                    0,
                    long.MaxValue,
                    this.BufferSize,
                    this.BufferCountPerNotification * this.BufferSize);
                client.TotalSize = this.TotalSize;
                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,
                        this.DownloadPath,
                        maxSizePerThread * i,
                        endPoint);

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

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

            // Set the downloading status.
            this.Status = MultiThreadedWebDownloaderStatus.Downloading;

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

                // Register the events of HttpDownloadClients.
                client.DownloadProgressChanged +=
                    new EventHandler<HttpDownloadClientProgressChangedEventArgs>(
                        client_DownloadProgressChanged);

                client.StatusChanged += new EventHandler(client_StatusChanged);

                client.ErrorOccurred += new EventHandler<ErrorEventArgs>(
                    client_ErrorOccurred);
                client.Start();
            }


        }