Exemplo n.º 1
0
        private void ManagerThread()
        {
            var block = Info.DownloadBlockList[_completedThread + ThreadNum - 1];

            if (urlNum >= Url.Length)
            {
                urlNum = 0;
            }
            var threadItem = new DownloadThread()
            {
                ID          = _completedThread + ThreadNum - 1,
                DownloadUrl = Url[urlNum],
                Path        = DownloadPath,
                Block       = block,
                Info        = Info,
            };

            threadItem.ThreadCompletedEvent += HttpDownload_ThreadCompletedEvent;
            urlNum++;
            _threads.Add(threadItem);
        }
Exemplo n.º 2
0
        public void Start()
        {
            if (Url == null || Url.Length == 0 || ThreadNum < 1 || Info == null)
            {
                MessageBox.Show("下载参数出错!");
                return;
            }


            try
            {
                if (Info.IsCompleted)
                {
                    DownloadState      = DownloadStateEnum.Completed;
                    DownloadPercentage = 100F;
                    DownloadSpeed      = 0L;
                    return;
                }

                DownloadState = DownloadStateEnum.Downloading;
                var response = GetResponse();
                if (response == null)
                {
                    DownloadState = DownloadStateEnum.Faulted;
                    return;
                }

                CreateDownloadPath(response.ContentLength);


                new Thread(ReportDownloadProgress).Start();


                var num           = 0;
                var leftTaskCount = (ThreadNum + _completedThread > Info.DownloadBlockList.Count ? Info.DownloadBlockList.Count : ThreadNum + _completedThread);
                for (int i = _completedThread; i < leftTaskCount; i++)
                {
                    var block = Info.DownloadBlockList[i];

                    if (block.IsCompleted)
                    {
                        continue;
                    }

                    if (num >= Url.Length)
                    {
                        num = 0;
                    }
                    var threadItem = new DownloadThread()
                    {
                        ID          = i,
                        DownloadUrl = Url[num],
                        Path        = DownloadPath,
                        Block       = block,
                        Info        = Info,
                    };
                    threadItem.ThreadCompletedEvent += HttpDownload_ThreadCompletedEvent;
                    _threads.Add(threadItem);

                    num++;
                }
            }
            catch (Exception ex)
            {
                DownloadState = DownloadStateEnum.Faulted;
            }
        }