private void StartNextFileItemDownload(File_DownloadItemViewModel fileItem)
        {
            if (downloaderType == DownloaderType.停止)
            {
                // 停止之後不能馬上加入不然
                // 有可能會再開新執行序下載
                // 清空所有列表
                lock (thisLock)
                {
                    list.Clear();
                }
            }
            // 設定成 沒有正在下載 和 沒有正在下載的物件
            //string MeetingID = fileItem.MeetingID;
            NowFileItem    = null;
            downloaderType = DownloaderType.沒有任何檔案下載中;
            // 不同線程的回呼,不會造成Recursive堆疊上限
            // 如果不是停止就開始新的檔案物件下載


            //FileDownloader fileDownloader = Singleton_FileDownloader.GetInstance(MeetingID);
            if (this.downloaderType == DownloaderType.沒有任何檔案下載中)
            {
                //ThreadPool.QueueUserWorkItem(callback => { this.Start(); });
                Task.Factory.StartNew(() => this.Start(), TaskCreationOptions.LongRunning);
            }
        }
        public LawDownloader()
        {
            downloaderType = DownloaderType.沒有任何檔案下載中;
            NowLawItem     = null;
            int downloadBuffer_KB = Settings.Default.DownloadBuffer_KB;

            buffer = 1024 * downloadBuffer_KB;
        }
        // 建構子
        public LawDownloader()
        {
            this.downloaderType = DownloaderType.沒有任何檔案下載中;
            this.NowLawItem     = null;
            // 依設定檔改變buffer
            int KB = PaperLess_Emeeting.Properties.Settings.Default.DownloadBuffer_KB;

            buffer = 1024 * KB;
        }
        //public string MeetingID { get; set; }

        //建構子
        public FileDownloader()
        {
            //this.MeetingID = MeetingID;
            this.downloaderType = DownloaderType.沒有任何檔案下載中;
            this.NowFileItem    = null;
            // 依設定檔改變buffer
            int KB = PaperLess_Emeeting.Properties.Settings.Default.DownloadBuffer_KB;

            buffer = 1024 * KB;
        }
Exemple #5
0
 public void Stop()
 {
     lock (this)
     {
         list.Clear();
     }
     if (downloaderType == DownloaderType.正在下載中)
     {
         downloaderType = DownloaderType.停止;
     }
 }
Exemple #6
0
        /// <summary>
        /// 根据传入的DownloaderType获取对应Downloader
        /// </summary>
        public static AbstractDownloader GetDownloaderFromType(DownloaderType type)
        {
            switch (type)
            {
            case DownloaderType.HttpSystemDownloader: return(new HttpSystemDownloader());

            case DownloaderType.HttpDownloader: return(new HttpDownloader());

            default: return(null);
            }
        }
 public void Stop()
 {
     lock (this)
     {
         list.Clear();
         if (downloaderType != 0)
         {
             downloaderType = DownloaderType.停止;
         }
     }
 }
        public DownloadManager(TaskQueue queue, DownloaderType type, int downloadCount = 0)
        {
            if (queue == null)
            {
                throw new ArgumentNullException(nameof(queue));
            }

            this.Type            = type;
            this.DownloaderCount = downloadCount > 0 ? downloadCount : DEFAULT_DOWNLOADER_COUNT;
            this.TaskQueue       = queue;
            this.Initialize();
        }
        public void Stop()
        {
            lock (this)
            {
                list.Clear();

                if (this.downloaderType != DownloaderType.沒有任何檔案下載中)
                {
                    downloaderType = DownloaderType.停止;
                }
            }
        }
        public void Pause(string fileItem_ID)
        {
            lock (this)
            {
                // 要先移除還沒下載的才暫停
                list.RemoveAll(x => x.ID.Equals(fileItem_ID));

                if (NowFileItem != null && NowFileItem.ID.Equals(fileItem_ID) == true)
                {
                    downloaderType = DownloaderType.暫停;
                }
            }
        }
        public static DownloaderBase Create(DownloaderType type)
        {
            switch (type)
            {
            case DownloaderType.Default:
                return(new PictureDownloader());

            case DownloaderType.Picture:
                return(new PictureDownloader());

            case DownloaderType.Video:
                return(new StreamDownloader());

            default:
                return(new PictureDownloader());
            }
        }
Exemple #12
0
        public void Pause(string fileItem_ID)
        {
            Predicate <File_DownloadItemViewModel> predicate = null;
            _003C_003Ec__DisplayClasse             _003C_003Ec__DisplayClasse = new _003C_003Ec__DisplayClasse();

            _003C_003Ec__DisplayClasse.fileItem_ID = fileItem_ID;
            lock (this)
            {
                List <File_DownloadItemViewModel> obj = list;
                if (predicate == null)
                {
                    predicate = new Predicate <File_DownloadItemViewModel>(_003C_003Ec__DisplayClasse._003CPause_003Eb__c);
                }
                obj.RemoveAll(predicate);
                if (NowFileItem != null && NowFileItem.ID.Equals(_003C_003Ec__DisplayClasse.fileItem_ID))
                {
                    downloaderType = DownloaderType.暫停;
                }
            }
        }
        public void Pause(string lawItem_ID)
        {
            Predicate <Law_DownloadItemViewModel> predicate = null;
            _003C_003Ec__DisplayClassd            _003C_003Ec__DisplayClassd = new _003C_003Ec__DisplayClassd();

            _003C_003Ec__DisplayClassd.lawItem_ID = lawItem_ID;
            lock (this)
            {
                List <Law_DownloadItemViewModel> obj = list;
                if (predicate == null)
                {
                    predicate = new Predicate <Law_DownloadItemViewModel>(_003C_003Ec__DisplayClassd._003CPause_003Eb__b);
                }
                obj.RemoveAll(predicate);
                if (NowLawItem != null && NowLawItem.ID.Equals(_003C_003Ec__DisplayClassd.lawItem_ID))
                {
                    downloaderType = DownloaderType.暫停;
                }
            }
        }
 private void StartNextFileItemDownload(Law_DownloadItemViewModel lawItem)
 {
     lock (this)
     {
         if (downloaderType == DownloaderType.停止)
         {
             //清空所有列表
             list.Clear();
         }
         // 設定成 沒有正在下載 和 沒有正在下載的物件
         lawItem        = null;
         downloaderType = DownloaderType.沒有任何檔案下載中;
         // 不同線程的回呼,不會造成Recursive堆疊上限
         // 如果不是停止就開始新的檔案物件下載
         //LawDownloader lawDownloader = Singleton_LawDownloader.GetInstance();
         if (this.downloaderType == DownloaderType.沒有任何檔案下載中)
         {
             //ThreadPool.QueueUserWorkItem(callback => { this.Start(); });
             Task.Factory.StartNew(() => this.Start(), TaskCreationOptions.LongRunning);
         }
     }
 }
        private void StartNextFileItemDownload(Law_DownloadItemViewModel lawItem)
        {
            Action action = null;

            lock (this)
            {
                if (downloaderType == DownloaderType.停止)
                {
                    list.Clear();
                }
                lawItem        = null;
                downloaderType = DownloaderType.沒有任何檔案下載中;
                if (downloaderType == DownloaderType.沒有任何檔案下載中)
                {
                    TaskFactory factory = Task.Factory;
                    if (action == null)
                    {
                        action = new Action(_003CStartNextFileItemDownload_003Eb__2);
                    }
                    factory.StartNew(action, TaskCreationOptions.LongRunning);
                }
            }
        }
Exemple #16
0
        public void Start()
        {
            downloaderType = DownloaderType.正在下載中;
            try
            {
                HttpWebRequest httpWebRequest;
                if (list.Count > 0)
                {
                    lock (thisLock)
                    {
                        if (list.Count > 0)
                        {
                            NowFileItem = list[0];
                            list.Remove(NowFileItem);
                        }
                    }
                    if (NowFileItem != null)
                    {
                        DownloadFileStart(NowFileItem);
                        httpWebRequest                        = (HttpWebRequest)WebRequest.Create(NowFileItem.Url);
                        httpWebRequest.CachePolicy            = new RequestCachePolicy(RequestCacheLevel.Revalidate);
                        httpWebRequest.Proxy                  = null;
                        httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
                        if (NowFileItem.DownloadBytes <= 0)
                        {
                            goto IL_013a;
                        }
                        if (!File.Exists(NowFileItem.StorageFilePath))
                        {
                            httpWebRequest.AddRange(NowFileItem.DownloadBytes);
                            goto IL_013a;
                        }
                        NowFileItem.DownloadBytes = new FileInfo(NowFileItem.StorageFilePath).Length;
                        if (NowFileItem.DownloadBytes < NowFileItem.TotalBytes)
                        {
                            httpWebRequest.AddRange(NowFileItem.DownloadBytes);
                            goto IL_013a;
                        }
                        downloaderType = DownloaderType.檔案下載完成;
                    }
                    else
                    {
                        downloaderType = DownloaderType.沒有任何檔案下載中;
                    }
                }
                else
                {
                    downloaderType = DownloaderType.沒有任何檔案下載中;
                }
                goto end_IL_0007;
IL_013a:
                WebResponse response = httpWebRequest.GetResponse();
                if (NowFileItem.TotalBytes == 0)
                {
                    NowFileItem.TotalBytes = response.ContentLength;
                }
                Directory.CreateDirectory(NowFileItem.StorageFileFolder);
                using (FileStream fileStream = new FileStream(NowFileItem.StorageFilePath, FileMode.OpenOrCreate))
                {
                    try
                    {
                        fileStream.Seek(0L, SeekOrigin.End);
                        using (Stream stream = response.GetResponseStream())
                        {
                            while (NowFileItem.DownloadBytes < NowFileItem.TotalBytes)
                            {
                                if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                {
                                    UpdateToDB(NowFileItem);
                                    break;
                                }
                                byte[] array = new byte[buffer];
                                int    num   = stream.Read(array, 0, array.Length);
                                if (num > 0)
                                {
                                    fileStream.Write(array, 0, num);
                                    fileStream.Flush();
                                    NowFileItem.DownloadBytes += num;
                                }
                                if (NowFileItem.DownloadBytes >= NowFileItem.TotalBytes)
                                {
                                    downloaderType = DownloaderType.檔案下載完成;
                                    break;
                                }
                                if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                {
                                    UpdateToDB(NowFileItem);
                                    break;
                                }
                                double percent = GetPercent(NowFileItem.DownloadBytes, NowFileItem.TotalBytes);
                                if (this.MeetingRoom_DownloadFileStart_Event != null)
                                {
                                    this.MeetingRoom_DownloadFileStart_Event(NowFileItem);
                                }
                                if (percent - NowFileItem.LastPercentage > (double)Settings.Default.Downloader_InvokePercent)
                                {
                                    NowFileItem.LastPercentage = percent;
                                    DownloadProgressChanged(NowFileItem);
                                    UpdateToDB(NowFileItem);
                                }
                            }
                            if (NowFileItem.DownloadBytes >= NowFileItem.TotalBytes)
                            {
                                DownloadProgressChanged(NowFileItem);
                                downloaderType = DownloaderType.檔案下載完成;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        downloaderType = DownloaderType.出錯;
                        DownloadError(NowFileItem);
                        LogTool.Debug(ex);
                    }
                }
                end_IL_0007 :;
            }
            catch (Exception ex2)
            {
                downloaderType = DownloaderType.出錯;
                DownloadError(NowFileItem);
                LogTool.Debug(ex2);
            }
            switch (downloaderType)
            {
            case DownloaderType.沒有任何檔案下載中:
            case (DownloaderType)1:
            case DownloaderType.正在下載中:
                break;

            case DownloaderType.停止:
                StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.出錯:
                downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.暫停:
                StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.檔案下載完成:
                DownloadFileCompleted(NowFileItem);
                StartNextFileItemDownload(NowFileItem);
                break;
            }
        }
        //這裡的Start請開執行序去跑
        public void Start()
        {
            downloaderType = DownloaderType.正在下載中;
            try
            {
                if (list.Count > 0)
                {
                    lock (thisLock)
                    {
                        if (list.Count > 0)
                        {
                            NowFileItem = list[0];
                            list.Remove(NowFileItem);
                        }
                    }
                    if (NowFileItem != null)
                    {
                        DownloadFileStart(NowFileItem); //開始

                        HttpWebRequest request = (HttpWebRequest)FileWebRequest.Create(NowFileItem.Url);
                        //Wayne 20150429
                        request.CachePolicy            = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Revalidate);
                        request.Proxy                  = null;
                        request.AutomaticDecompression = DecompressionMethods.GZip;
                        // 不要使用這個,取得或設定值,指出是否要分區段傳送資料至網際網路資源。
                        //request.SendChunked = true;
                        if (NowFileItem.DownloadBytes > 0)
                        {
                            if (File.Exists(NowFileItem.StorageFilePath) == true)
                            {
                                NowFileItem.DownloadBytes = new FileInfo(NowFileItem.StorageFilePath).Length;
                                if (NowFileItem.DownloadBytes >= NowFileItem.TotalBytes)
                                {
                                    downloaderType = DownloaderType.檔案下載完成;
                                    // 更新資料
                                    //UpdateToDB(NowFileItem);
                                    goto TypeControl;
                                }
                                else
                                {
                                    request.AddRange(NowFileItem.DownloadBytes);
                                    //UpdateToDB(NowFileItem);
                                }
                            }
                            else
                            {
                                request.AddRange(NowFileItem.DownloadBytes);
                                // 更新資料
                                //UpdateToDB(NowFileItem);
                            }
                        }

                        //取得回應
                        WebResponse response = request.GetResponse();

                        if (NowFileItem.TotalBytes == 0)
                        {
                            NowFileItem.TotalBytes = response.ContentLength;
                        }

                        //先建立檔案要存放的資料夾
                        Directory.CreateDirectory(NowFileItem.StorageFileFolder);

                        //FileMode fileMode = FileMode.Create;
                        //if (File.Exists(NowFileItem.StorageFilePath) == true)
                        //{
                        //    fileMode = FileMode.Open;
                        //}

                        using (var writer = new FileStream(NowFileItem.StorageFilePath, FileMode.OpenOrCreate))
                        {
                            try
                            {
                                writer.Seek(0, SeekOrigin.End);
                                using (var stream = response.GetResponseStream())
                                {
                                    while (NowFileItem.DownloadBytes < NowFileItem.TotalBytes)  //暫停條件設定成 fileItem的屬性 IsPause =true 就停止
                                    {
                                        // 暫停和停止動作一定要寫在while裡
                                        if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                        {
                                            // 可存可不存,各有優缺點
                                            // 存起來的話缺點為user暫停後,馬上重整可能會多1%進度
                                            // 不存的話不會有以上缺點,因為AddRange是從FileStream裡面讀取的所以不影響
                                            UpdateToDB(NowFileItem);
                                            break;
                                        }

                                        byte[] data       = new byte[buffer];
                                        int    readNumber = stream.Read(data, 0, data.Length);

                                        if (readNumber > 0)
                                        {
                                            writer.Write(data, 0, readNumber);
                                            writer.Flush();
                                            NowFileItem.DownloadBytes += readNumber;
                                        }

                                        if (NowFileItem.DownloadBytes >= NowFileItem.TotalBytes)
                                        {
                                            //先寫入檔案,並且關閉檔案使用權。
                                            downloaderType = DownloaderType.檔案下載完成;
                                            //UpdateToDB(NowFileItem);
                                            break;
                                        }
                                        else
                                        {
                                            if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                            {
                                                UpdateToDB(NowFileItem);
                                                break;
                                            }
                                            else
                                            {
                                                double percentage = GetPercent(NowFileItem.DownloadBytes, NowFileItem.TotalBytes);

                                                // 加速MeetingRoom的事件接收,才不會讓MeetingRoom的下載事件開始的觸發
                                                // 被下面的1%的限制給延後觸發MeetingRoom的開始下載。
                                                if (MeetingRoom_DownloadFileStart_Event != null)
                                                {
                                                    MeetingRoom_DownloadFileStart_Event(NowFileItem);
                                                }

                                                if (percentage - NowFileItem.LastPercentage > PaperLess_Emeeting.Properties.Settings.Default.Downloader_InvokePercent)
                                                {
                                                    NowFileItem.LastPercentage = percentage;
                                                    // 進度條百分比callback
                                                    DownloadProgressChanged(NowFileItem);
                                                    UpdateToDB(NowFileItem);
                                                    //Thread.Sleep(1);
                                                }
                                            }
                                        }
                                    }

                                    if (NowFileItem.DownloadBytes >= NowFileItem.TotalBytes)
                                    {
                                        DownloadProgressChanged(NowFileItem);
                                        //先寫入檔案,並且關閉檔案使用權。
                                        downloaderType = DownloaderType.檔案下載完成;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                downloaderType = DownloaderType.出錯;
                                DownloadError(NowFileItem);
                                LogTool.Debug(ex);
                            }
                        }
                    }
                    else
                    {
                        downloaderType = DownloaderType.沒有任何檔案下載中;
                    }
                }
                else
                {
                    downloaderType = DownloaderType.沒有任何檔案下載中;
                }
            }
            catch (Exception ex)
            {
                downloaderType = DownloaderType.出錯;
                DownloadError(NowFileItem);
                LogTool.Debug(ex);
            }



TypeControl:
            // 在這裡統整所有的控制行為 錯誤,暫停,停止,完成
            switch (downloaderType)
            {
            case DownloaderType.沒有任何檔案下載中:
                //StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.停止:
                // 呼叫 Stop callback
                // 停止可以再呼叫一次StartNextFileItemDownload(NowFileItem);
                // 避免下載器的狀態跑到停止後,可能會沒有被重置成 DownloaderType.沒有任何檔案下載中;
                //NowFileItem = null;
                //lock (thisLock)
                //{
                //    list.Clear();
                //}
                //downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowFileItem);
                // 不要開新的下載
                break;

            case DownloaderType.出錯:
                // 呼叫 Error callback
                // 儲存下載狀態
                downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.暫停:
                // 呼叫 Pause callback
                StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.檔案下載完成:
                // 呼叫 FileCompleted callback
                DownloadFileCompleted(NowFileItem);
                StartNextFileItemDownload(NowFileItem);
                break;
            }
        }
        //這裡的Start請開執行緒去跑
        public void Start()
        {
            downloaderType = DownloaderType.正在下載中;
            try
            {
                if (list.Count > 0)
                {
                    lock (thisLock)
                    {
                        if (list.Count > 0)
                        {
                            NowLawItem = list[0];
                            list.Remove(NowLawItem);
                        }
                    }
                    if (NowLawItem != null)
                    {
                        try
                        {
                            DownloadFileStart(NowLawItem); //開始

                            HttpWebRequest request = (HttpWebRequest)FileWebRequest.Create(NowLawItem.Link);

                            //Wayne 20150429
                            request.CachePolicy            = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Revalidate);
                            request.Proxy                  = null;
                            request.AutomaticDecompression = DecompressionMethods.GZip;

                            if (NowLawItem.DownloadBytes > 0)
                            {
                                //if (File.Exists(NowLawItem.StorageFilePath) == true)
                                //{
                                //    NowLawItem.DownloadBytes = new FileInfo(NowLawItem.StorageFilePath).Length;
                                //    if (NowLawItem.DownloadBytes >= NowLawItem.TotalBytes)
                                //    {
                                //        downloaderType = DownloaderType.檔案下載完成;
                                //        // 更新資料
                                //        //UpdateToDB(NowFileItem);
                                //        goto TypeControl;
                                //    }
                                //    else
                                //    {
                                //        request.AddRange(NowLawItem.DownloadBytes);
                                //        //UpdateToDB(NowFileItem);
                                //    }
                                //}
                                //else
                                //{
                                //request.AddRange(NowLawItem.DownloadBytes);
                                // 更新資料
                                //UpdateToDB(NowFileItem);
                                //}
                            }

                            //取得回應
                            WebResponse response = request.GetResponse();

                            if (NowLawItem.TotalBytes == 0)
                            {
                                NowLawItem.TotalBytes = response.ContentLength;
                            }

                            //先建立檔案要存放的資料夾
                            Directory.CreateDirectory(NowLawItem.StorageFileFolder);
                            using (var writer = new FileStream(NowLawItem.StorageFilePath, FileMode.OpenOrCreate))
                            {
                                try
                                {
                                    writer.Seek(0, SeekOrigin.End);
                                    using (var stream = response.GetResponseStream())
                                    {
                                        while (NowLawItem.DownloadBytes < NowLawItem.TotalBytes) //暫停條件設定成 fileItem的屬性 IsPause =true 就停止
                                        {
                                            // 暫停和停止動作一定要寫在while裡
                                            if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                            {
                                                //UpdateToDB(NowLawItem);
                                                break;
                                            }

                                            byte[] data       = new byte[buffer];
                                            int    readNumber = stream.Read(data, 0, data.Length);
                                            if (readNumber > 0)
                                            {
                                                writer.Write(data, 0, readNumber);
                                                writer.Flush();
                                                NowLawItem.DownloadBytes += readNumber;
                                            }

                                            if (NowLawItem.DownloadBytes >= NowLawItem.TotalBytes)
                                            {
                                                //先寫入檔案,並且關閉檔案使用權。
                                                downloaderType = DownloaderType.檔案下載完成;
                                                break;
                                            }
                                            else
                                            {
                                                if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                                {
                                                    //UpdateToDB(NowLawItem);
                                                    break;
                                                }
                                                else
                                                {
                                                    double percentage = GetPercent(NowLawItem.DownloadBytes, NowLawItem.TotalBytes);
                                                    if (percentage - NowLawItem.LastPercentage > PaperLess_Emeeting.Properties.Settings.Default.Downloader_InvokePercent)
                                                    {
                                                        NowLawItem.LastPercentage = percentage;
                                                        // 進度條百分比callback
                                                        DownloadProgressChanged(NowLawItem);
                                                        //UpdateToDB(NowLawItem);
                                                        //Thread.Sleep(1);
                                                    }
                                                }
                                            }
                                        }

                                        if (NowLawItem.DownloadBytes >= NowLawItem.TotalBytes)
                                        {
                                            DownloadProgressChanged(NowLawItem);
                                            //先寫入檔案,並且關閉檔案使用權。
                                            downloaderType = DownloaderType.檔案下載完成;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    downloaderType = DownloaderType.出錯;
                                    try
                                    {
                                        if (Home_UnZipError_Event != null)
                                        {
                                            Home_UnZipError_Event(string.Format("檔名: {0},{1}"
                                                                                , NowLawItem.Name == null ? "" : NowLawItem.Name
                                                                                , Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                                            Thread.Sleep(1100);
                                        }
                                        LogTool.Debug(ex);
                                    }
                                    catch
                                    {
                                        downloaderType = DownloaderType.出錯;
                                    }
                                }
                            }
                        }
                        catch (Exception ex2)
                        {
                            downloaderType = DownloaderType.出錯;
                            try
                            {
                                if (Home_UnZipError_Event != null)
                                {
                                    Home_UnZipError_Event(string.Format("檔名: {0},{1}"
                                                                        , NowLawItem.Name == null ? "" : NowLawItem.Name
                                                                        , Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                                    Thread.Sleep(1100);
                                }
                                LogTool.Debug(ex2);
                            }
                            catch
                            {
                                downloaderType = DownloaderType.出錯;
                            }
                        }
                    }
                    else
                    {
                        downloaderType = DownloaderType.沒有任何檔案下載中;
                    }
                }
                else
                {
                    downloaderType = DownloaderType.沒有任何檔案下載中;
                }
            }
            catch (Exception ex)
            {
                downloaderType = DownloaderType.出錯;
                try
                {
                    if (Home_UnZipError_Event != null)
                    {
                        Home_UnZipError_Event(string.Format("檔名: {0},{1}"
                                                            , NowLawItem.Name == null ? "" : NowLawItem.Name
                                                            , Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                        Thread.Sleep(1100);
                    }
                    LogTool.Debug(ex);
                }
                catch
                {
                    downloaderType = DownloaderType.出錯;
                }
            }

TypeControl:
            // 在這裡統整所有的控制行為 錯誤,暫停,停止,完成
            switch (downloaderType)
            {
            case DownloaderType.沒有任何檔案下載中:
                //StartNextFileItemDownload(NowFileItem);
                break;

            case DownloaderType.停止:
                //NowLawItem = null;
                //lock (thisLock)
                //{
                //    list.Clear();
                //}
                //downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowLawItem);
                // 不要開新的下載
                break;

            case DownloaderType.出錯:
                // 呼叫 Error callback
                downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowLawItem);
                break;

            case DownloaderType.暫停:
                // 呼叫 Pause callback
                StartNextFileItemDownload(NowLawItem);
                break;

            case DownloaderType.檔案下載完成:
                // 呼叫 FileCompleted callback
                // 下面會等待到解壓縮完,才會繼續下一個檔案的下載
                DownloadFileCompleted(NowLawItem);
                StartNextFileItemDownload(NowLawItem);
                break;
            }
        }
        public void Start()
        {
            downloaderType = DownloaderType.正在下載中;
            try
            {
                if (list.Count > 0)
                {
                    lock (thisLock)
                    {
                        if (list.Count > 0)
                        {
                            NowLawItem = list[0];
                            list.Remove(NowLawItem);
                        }
                    }
                    if (NowLawItem != null)
                    {
                        try
                        {
                            DownloadFileStart(NowLawItem);
                            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(NowLawItem.Link);
                            httpWebRequest.CachePolicy            = new RequestCachePolicy(RequestCacheLevel.Revalidate);
                            httpWebRequest.Proxy                  = null;
                            httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
                            long        downloadByte = NowLawItem.DownloadBytes;
                            int         num2         = 0;
                            WebResponse response     = httpWebRequest.GetResponse();
                            if (NowLawItem.TotalBytes == 0)
                            {
                                NowLawItem.TotalBytes = response.ContentLength;
                            }
                            Directory.CreateDirectory(NowLawItem.StorageFileFolder);
                            using (FileStream fileStream = new FileStream(NowLawItem.StorageFilePath, FileMode.OpenOrCreate))
                            {
                                try
                                {
                                    fileStream.Seek(0L, SeekOrigin.End);
                                    using (Stream stream = response.GetResponseStream())
                                    {
                                        while (NowLawItem.DownloadBytes < NowLawItem.TotalBytes && downloaderType != DownloaderType.暫停 && downloaderType != DownloaderType.停止)
                                        {
                                            byte[] array = new byte[buffer];
                                            int    num   = stream.Read(array, 0, array.Length);
                                            if (num > 0)
                                            {
                                                fileStream.Write(array, 0, num);
                                                fileStream.Flush();
                                                NowLawItem.DownloadBytes += num;
                                            }
                                            if (NowLawItem.DownloadBytes >= NowLawItem.TotalBytes)
                                            {
                                                downloaderType = DownloaderType.檔案下載完成;
                                                break;
                                            }
                                            if (downloaderType == DownloaderType.暫停 || downloaderType == DownloaderType.停止)
                                            {
                                                break;
                                            }
                                            double percent = GetPercent(NowLawItem.DownloadBytes, NowLawItem.TotalBytes);
                                            if (percent - NowLawItem.LastPercentage > (double)Settings.Default.Downloader_InvokePercent)
                                            {
                                                NowLawItem.LastPercentage = percent;
                                                DownloadProgressChanged(NowLawItem);
                                            }
                                        }
                                        if (NowLawItem.DownloadBytes >= NowLawItem.TotalBytes)
                                        {
                                            DownloadProgressChanged(NowLawItem);
                                            downloaderType = DownloaderType.檔案下載完成;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    downloaderType = DownloaderType.出錯;
                                    try
                                    {
                                        if (this.Home_UnZipError_Event != null)
                                        {
                                            this.Home_UnZipError_Event(string.Format("檔名: {0},{1}", (NowLawItem.Name == null) ? "" : NowLawItem.Name, Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                                            Thread.Sleep(1100);
                                        }
                                        LogTool.Debug(ex);
                                    }
                                    catch
                                    {
                                        downloaderType = DownloaderType.出錯;
                                    }
                                }
                            }
                        }
                        catch (Exception ex2)
                        {
                            downloaderType = DownloaderType.出錯;
                            try
                            {
                                if (this.Home_UnZipError_Event != null)
                                {
                                    this.Home_UnZipError_Event(string.Format("檔名: {0},{1}", (NowLawItem.Name == null) ? "" : NowLawItem.Name, Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                                    Thread.Sleep(1100);
                                }
                                LogTool.Debug(ex2);
                            }
                            catch
                            {
                                downloaderType = DownloaderType.出錯;
                            }
                        }
                    }
                    else
                    {
                        downloaderType = DownloaderType.沒有任何檔案下載中;
                    }
                }
                else
                {
                    downloaderType = DownloaderType.沒有任何檔案下載中;
                }
            }
            catch (Exception ex3)
            {
                downloaderType = DownloaderType.出錯;
                try
                {
                    if (this.Home_UnZipError_Event != null)
                    {
                        this.Home_UnZipError_Event(string.Format("檔名: {0},{1}", (NowLawItem.Name == null) ? "" : NowLawItem.Name, Enum.GetName(typeof(DownloaderType), DownloaderType.出錯)));
                        Thread.Sleep(1100);
                    }
                    LogTool.Debug(ex3);
                }
                catch
                {
                    downloaderType = DownloaderType.出錯;
                }
            }
            switch (downloaderType)
            {
            case DownloaderType.沒有任何檔案下載中:
            case (DownloaderType)1:
            case DownloaderType.正在下載中:
                break;

            case DownloaderType.停止:
                StartNextFileItemDownload(NowLawItem);
                break;

            case DownloaderType.出錯:
                downloaderType = DownloaderType.沒有任何檔案下載中;
                StartNextFileItemDownload(NowLawItem);
                break;

            case DownloaderType.暫停:
                StartNextFileItemDownload(NowLawItem);
                break;

            case DownloaderType.檔案下載完成:
                DownloadFileCompleted(NowLawItem);
                StartNextFileItemDownload(NowLawItem);
                break;
            }
        }