Beispiel #1
0
        /// <summary>
        ///     下载文件方法(自动确定是否使用分片下载)
        /// </summary>
        /// <param name="fileEnumerable">文件列表</param>
        /// <param name="downloadParts"></param>
        public static async Task AdvancedDownloadListFile(IEnumerable <DownloadFile> fileEnumerable,
                                                          int downloadParts = 16)
        {
            ProcessorHelper.SetMaxThreads();

            var filesBlock =
                new TransformManyBlock <IEnumerable <DownloadFile>, DownloadFile>(d =>
            {
                var dl = d.ToList();

                foreach (var df in dl.Where(df => !Directory.Exists(df.DownloadPath)))
                {
                    Directory.CreateDirectory(df.DownloadPath);
                }

                return(dl);
            });

            var actionBlock = new ActionBlock <DownloadFile>(async d =>
            {
                if (d.FileSize is >= 1048576 or 0)
                {
                    await MultiPartDownloadTaskAsync(d, downloadParts);
                }
Beispiel #2
0
        /// <summary>
        ///     下载文件方法(自动确定是否使用分片下载)
        /// </summary>
        /// <param name="fileEnumerable">文件列表</param>
        /// <param name="downloadThread">下载线程</param>
        /// <param name="tokenSource"></param>
        public static async Task AdvancedDownloadListFile(IEnumerable <DownloadFile> fileEnumerable, int downloadThread,
                                                          CancellationTokenSource tokenSource, int downloadParts = 16)
        {
            var downloadFiles  = fileEnumerable.ToList();
            var token          = tokenSource?.Token ?? CancellationToken.None;
            var processorCount = ProcessorHelper.GetPhysicalProcessorCount();

            if (downloadThread <= 0)
            {
                downloadThread = processorCount;
            }

            using var bc = new BlockingCollection <DownloadFile>(downloadThread * 4);
            using var downloadQueueTask = Task.Run(() =>
            {
                foreach (var df in downloadFiles)
                {
                    bc.Add(df, token);
                }

                bc.CompleteAdding();
            }, token);

            using var downloadTask = Task.Run(() =>
            {
                void DownloadAction()
                {
                    foreach (var df in bc.GetConsumingEnumerable())
                    {
                        var di = new DirectoryInfo(
                            df.DownloadPath.Substring(0, df.DownloadPath.LastIndexOf('\\')));
                        if (!di.Exists)
                        {
                            di.Create();
                        }

                        // DownloadData(df);
                        if (df.FileSize >= 1048576 || df.FileSize == 0) // || df.FileSize == default)
                        {
                            MultiPartDownload(df, downloadParts);
                        }
                        else
                        {
                            DownloadData(df);
                        }
                    }
                }

                var threads = new List <Thread>();

                for (var i = 0; i < downloadThread * 2; i++)
                {
                    threads.Add(new Thread(DownloadAction));
                }

                foreach (var t in threads)
                {
                    t.Start();
                }

                foreach (var t in threads)
                {
                    t.Join();
                }
            }, token);

            await Task.WhenAll(downloadQueueTask, downloadTask).ConfigureAwait(false);
        }