Exemple #1
0
        /// <summary>
        /// 开始下载文件
        /// </summary>
        /// <param name="listener">监听下载数量的变化,如果不需要了解实时下载的数量,可以设置为null</param>
        /// <returns>已下载文件大小</returns>
        public long download(IDownloadProgressListener listener)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            try
            {
                using (FileStream fstream = new FileStream(this.saveFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    if (this.fileSize > 0)
                    {
                        fstream.SetLength(this.fileSize);
                    }
                    fstream.Close();
                }
                if (this.data.Count != this.threads.Length)
                {
                    this.data.Clear();
                    for (int i = 0; i < this.threads.Length; i++)
                    {
                        this.data.Add(i + 1, 0);//初始化每条线程已经下载的数据长度为0
                    }
                }
                for (int i = 0; i < this.threads.Length; i++)
                {//开启线程进行下载
                    long downLength = this.data[i + 1];
                    if (downLength < this.block && this.downloadSize < this.fileSize)
                    {//判断线程是否已经完成下载,否则继续下载	+
                        // Console.WriteLine("threads" + i.ToString() + ",下载块" + this.block.ToString() + "    " + this.data[i + 1].ToString() + "              " + downloadSize.ToString());
                        this.threads[i] = new DownloadThread(this, downloadUrl, this.saveFile, this.block, this.data[i + 1], i + 1);
                        this.threads[i].ThreadRun();
                    }
                    else
                    {
                        this.threads[i] = null;
                    }
                }
                bool notFinish = true; //下载未完成
                while (notFinish)
                {                      // 循环判断所有线程是否完成下载
                    Thread.Sleep(900);
                    notFinish = false; //假定全部线程下载完成
                    for (int i = 0; i < this.threads.Length; i++)
                    {
                        if (this.threads[i] != null && !this.threads[i].isFinish())
                        {                     //如果发现线程未完成下载
                            notFinish = true; //设置标志为下载没有完成
                            if (this.threads[i].getDownLength() == -1)
                            {                 //如果下载失败,再重新下载
                                this.threads[i] = new DownloadThread(this, downloadUrl, this.saveFile, this.block, this.data[i + 1], i + 1);
                                this.threads[i].ThreadRun();
                            }
                        }
                    }
                    if (listener != null)
                    {
                        listener.OnDownloadSize(this.downloadSize);//通知目前已经下载完成的数据长度

                        //   Console.WriteLine(this.downloadSize);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new Exception("下载文件失败");
            }
            if (downloadSize == fileSize)
            {
                try { File.Move(saveFile, saveFile.Replace(".Square", "")); } catch (Exception) { File.Delete(saveFile); }
            }
            return(this.downloadSize);
        }
 /// <summary>
 /// 开始下载文件
 /// </summary>
 /// <param name="listener">监听下载数量的变化,如果不需要了解实时下载的数量,可以设置为null</param>
 /// <returns>已下载文件大小</returns>
 public long DownloadAction(IDownloadProgressListener listener)
 {
     try
     {
         using (FileStream fstream = new FileStream(this.saveFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
         {
             if (this.fileSize > 0)
             {
                 fstream.SetLength(this.fileSize);
             }
             fstream.Close();
         }
         if (this.data.Count != this.threads.Length)
         {
             this.data.Clear();
             for (int i = 0; i < this.threads.Length; i++)
             {
                 this.data.Add(i + 1, 0);//初始化每条线程已经下载的数据长度为0
             }
         }
         //for (int i = 0; i < this.threads.Length; i++)//开启线程进行下载
         //{
         //    long downLength = this.data[i + 1];
         //    if (downLength < this.block && this.downloadSize < this.fileSize)//判断线程是否已经完成下载,否则继续下载	+
         //    {
         //        this.threads[i] = new DownloadThread(this, downloadUrl, this.saveFile, this.block, this.data[i + 1], i + 1);
         //        this.threads[i].ThreadRun();
         //    }
         //    else
         //    {
         //        this.threads[i] = null;
         //    }
         //}
         bool notFinish = true; //下载未完成
         while (notFinish)      // 循环判断所有线程是否完成下载,(可以加事件监听??)
         {
             Thread.Sleep(900);
             notFinish = false;//假定全部线程下载完成
             for (int i = 0; i < this.threads.Length; i++)
             {
                 if (this.threads[i] != null)//如果发现线程未完成下载
                 {
                     if (!this.threads[i].IsFinish())
                     {
                         notFinish = true;                          //设置标志为下载没有完成
                         if (this.threads[i].GetDownLength() == -1) //如果下载失败,再重新下载
                         {
                             this.threads[i] = new DownloadThread(this, downloadUrl, this.saveFile, this.block, this.data[i + 1], i + 1);
                             this.threads[i].ThreadRun();
                         }
                     }
                     else
                     {
                         this.threads[i] = null;
                     }
                 }
             }
             if (listener != null)
             {
                 listener.OnDownloadSize(this.downloadSize);//通知目前已经下载完成的数据长度
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         throw new Exception("下载文件失败");
     }
     return(this.downloadSize);
 }