/// <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.");
            }

            EnsurePropertyValid();

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

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

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

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

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

            downloadClients.Add(client);
            client.Download();
        }
        void DownloadInternal(object obj)
        {
            if (Status != DownloadStatus.Waiting)
            {
                return;
            }

            try
            {
                EnsurePropertyValid();

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

                if (!HasChecked)
                {
                    CheckUrlAndFile(out string 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(
                        Url.AbsoluteUri,
                        0,
                        long.MaxValue,
                        BufferSize,
                        BufferCountPerNotification * BufferSize,
                        BufferCountPerNotification)
                    {
                        // Set the HasChecked flag, so the client will not check the URL again.
                        TotalSize    = TotalSize,
                        DownloadPath = DownloadPath,
                        HasChecked   = true,
                        Credentials  = Credentials,
                        Proxy        = Proxy
                    };

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

                    long leftSizeToDownload = TotalSize;

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

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

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

                        // Download a block of the whole file.
                        HttpDownloadClient client = new HttpDownloadClient(
                            Url.AbsoluteUri,
                            maxSizePerThread * i,
                            endPoint)
                        {
                            // Set the HasChecked flag, so the client will not check the URL again.
                            DownloadPath = DownloadPath,
                            HasChecked   = true,
                            TotalSize    = sizeToDownload,
                            Credentials  = Credentials,
                            Proxy        = Proxy
                        };
                        downloadClients.Add(client);
                    }
                }

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

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

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


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