Exemplo n.º 1
0
        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="eType">类型</param>
        /// <param name="sUrl">连接</param>
        /// <param name="sFilePathName">文件名</param>
        /// <param name="iTimeOut">超时时间</param>
        /// <returns></returns>
        private static object Download(DOWNLOAD_TYPE eType, string sUrl, string sFilePathName, int iTimeOut)
        {
            object      aRet    = null;
            WebClientEx aClient = new WebClientEx(iTimeOut);

            try
            {
                WebRequest myre = WebRequest.Create(sUrl);
                if (eType == DOWNLOAD_TYPE.FILE)
                {
                    if (String.IsNullOrWhiteSpace(sFilePathName))
                    {
                        return(null);
                    }
                    var di = new DirectoryInfo(Path.GetDirectoryName(sFilePathName));
                    if (!di.Exists)
                    {
                        di.Create();
                    }

                    aClient.DownloadFile(sUrl, sFilePathName);
                    aRet = 0;
                }
                if (eType == DOWNLOAD_TYPE.STIRNG)
                {
                    aClient.Encoding = System.Text.Encoding.UTF8;//定义对象语言
                    aRet             = aClient.DownloadString(sUrl);
                }
                if (eType == DOWNLOAD_TYPE.DATA)
                {
                    aRet = aClient.DownloadData(sUrl);
                }
            }
            catch
            {
                aClient.Dispose();
                return(aRet);
            }

            aClient.Dispose();
            return(aRet);
        }
Exemplo n.º 2
0
        /// <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);
            }
        }