/// <summary> /// Main Download method /// </summary> /// <param name="fileName"></param> /// <param name="fileNameIndex"></param> /// <param name="totalCount"></param> /// <param name="AUTO_DECRYPT"></param> /// <returns></returns> private async Task DownloadFiles(string fileName, int fileNameIndex, int totalCount, AUTO_DECRYPT AUTO_DECRYPT) { SetNoticesText("正在下载 ... " + _downloadDoneList.Count + " / " + totalCount, downloadNotice); if (IsSeveralFiles) { _downloadProcessMessageHandler.HttpReceiveProgress += (senders, es) => { var num = es.ProgressPercentage; SetProgressInt(num); } } ; _downloadDoneList.Add(await _request.MakeGetRequest(Define.GetUrl(fileName), Define.GetFileSavePath(), fileName)); SetNoticesText("正在下载 ... " + _downloadDoneList.Count + " / " + totalCount, downloadNotice); if (!IsSeveralFiles) { ReloadProcess(totalCount); } if (AUTO_DECRYPT == AUTO_DECRYPT.Auto) { if (Save.GetFileType(fileName) != ENC_TYPE.ERROR) { DecryptFiles.DecryptFile(Define.GetFileSavePath() + fileName, Crypt.Crypt.IdentifyEncVersion(fileName)); } } OnDownloadDone(totalCount); }
private void Button_OpenDownloadPath_Click(object sender, EventArgs e) { if (!Directory.Exists(Define.GetFileSavePath())) { Directory.CreateDirectory(Define.GetFileSavePath()); } Process.Start("Explorer.exe", Define.GetFileSavePath()); }
/// <summary> /// Main control download method. /// </summary> /// <param name="downloadType">下载类型:少量下载或大量下载</param> private async void StartDownload(DOWNLOAD_TYPE downloadType) { if (!Directory.Exists(Define.GetFileSavePath())) { Directory.CreateDirectory(Define.GetFileSavePath()); } //Initialize the UI about download SetProgressInt(0); button_DownloadCancel.Visible = true; string[] willDownloadList; int totalCount; var nowFileIndex = 0; switch (downloadType) { case DOWNLOAD_TYPE.AllFiles: totalCount = _listResult.Length; willDownloadList = _listResult; break; case DOWNLOAD_TYPE.SeletFiles: totalCount = listBoxResult.SelectedItems.Count; if (totalCount < 1) { MessageBox.Show(@"未选择要下载的文件,请点击选择某项或多项文件,再开始下载。", "Notice"); button_DownloadCancel.Visible = false; return; } var checkedNameList = new string[totalCount]; for (var i = 0; i < totalCount; i++) { checkedNameList[i] = listBoxResult.SelectedItems[i].ToString(); } willDownloadList = checkedNameList; break; default: totalCount = _listResult.Length; willDownloadList = _listResult; break; } //Start Downlaod try { var cancelToken = CancelSource.Token; var scheduler = new LimitedConcurrencyLevelTaskScheduler(Define.MaxDownloadTasks); var downloadTaskFactory = new TaskFactory(scheduler); if (totalCount <= 15) { IsSeveralFiles = true; foreach (var fileName in willDownloadList) { await downloadTaskFactory.StartNew(async nowFileName => { nowFileIndex++; await DownloadFiles(fileName, nowFileIndex, totalCount, AUTO_DECRYPT.Auto); }, fileName); } } else { IsSeveralFiles = false; Define.DownloadTaskSleep = totalCount < 200 ? 100 : totalCount > 1000 ? 500 : totalCount / 3; foreach (var fileName in willDownloadList) { await downloadTaskFactory.StartNew(async nowFileName => { nowFileIndex++; Thread.Sleep(nowFileIndex % 25 == 0 ? 500 : Define.DownloadTaskSleep); await DownloadFiles(nowFileName.ToString(), nowFileIndex, totalCount, AUTO_DECRYPT.Auto); }, fileName, cancelToken); } } cancelToken.ThrowIfCancellationRequested(); } catch (Exception e) { MessageBox.Show(e.Message); } }