static async Task Main(string[] args) { await(await Parser.Default.ParseArguments <DownloadOption>(args).WithParsedAsync(DownloadFileAsync)).WithNotParsedAsync( async _ => { #if DEBUG var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); var logger = loggerFactory.CreateLogger <SegmentFileDownloader>(); // https://www.speedtest.cn/ var url = "https://speedtest1.gd.chinamobile.com.prod.hosts.ooklaserver.net:8080/download?size=25000000&r=0.2978374611691549"; url = "https://download.jetbrains.8686c.com/resharper/ReSharperUltimate.2020.1.3/JetBrains.ReSharperUltimate.2020.1.3.exe"; //var md5 = "7d6bbeb6617a7c0b7e615098fca1b167";// resharper var file = new FileInfo(@"File.txt"); var progress = new Progress <DownloadProgress>(); var segmentFileDownloader = new SegmentFileDownloader(url, file, logger, progress); await segmentFileDownloader.DownloadFile(); #endif await Task.Delay(100); }); }
public async Task LoadAsync(string url, int threadCount) { try { string StrFileName = DownPath + name; //根据实际情况设置 var file = new FileInfo(StrFileName); var progress = new Progress <DownloadProgress>(); progress.ProgressChanged += (sender, p) => { lock (obj) { var downloadProgress = p; downCount++; } }; var segmentFileDownloader = new SegmentFileDownloader(url, file, progress); await segmentFileDownloader.DownloadFile(); } catch (Exception) { throw; } finally { threadCount--; } }
private static async Task DownloadFileAsync(DownloadOption option) { var output = option.Output ?? ""; output = Path.GetFullPath(output); Console.WriteLine($"Download url = {option.Url}"); Console.WriteLine($"Output = {output}"); try { var loggerFactory = LoggerFactory.Create(builder => { }); var logger = loggerFactory.CreateLogger <SegmentFileDownloader>(); using var progress = new FileDownloadSpeedProgress(); progress.ProgressChanged += (sender, downloadProgress) => { Console.Clear(); Console.WriteLine($"Download url = {option.Url}"); Console.WriteLine($"Output = {output}"); Console.WriteLine(); Console.WriteLine( $"Process {downloadProgress.DownloadProcess} {downloadProgress.DownloadSpeed}"); foreach (var downloadSegment in downloadProgress.DownloadProgress.GetCurrentDownloadSegmentList()) { Console.WriteLine(downloadSegment); } }; var file = new FileInfo(output); var url = option.Url; var segmentFileDownloader = new SegmentFileDownloader(url, file, logger, progress); await segmentFileDownloader.DownloadFileAsync(); //finished = true; } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e); Console.ResetColor(); } }
public async void AddDownloadFile() { var url = AddFileDownloadViewModel.CurrentDownloadUrl; var file = AddFileDownloadViewModel.CurrentDownloadFilePath; var logger = _loggerFactory.CreateLogger <SegmentFileDownloader>(); using var progress = new FileDownloadSpeedProgress(); file = Path.GetFullPath(file); var downloadFileInfo = new DownloadFileInfo() { FileName = Path.GetFileName(file), AddedTime = DateTime.Now.ToString(), DownloadUrl = url, FilePath = file }; DownloadFileInfoList.Add(downloadFileInfo); progress.ProgressChanged += (sender, downloadProgress) => { downloadFileInfo.DownloadSpeed = downloadProgress.DownloadSpeed; downloadFileInfo.FileSize = downloadProgress.FileSize; downloadFileInfo.DownloadProcess = downloadProgress.DownloadProcess; }; _ = DownloadFileListManager.WriteDownloadedFileListToFileAsync(DownloadFileInfoList.ToList()); var segmentFileDownloader = new SegmentFileDownloader(url, new FileInfo(file), logger, progress, sharedArrayPool: SharedArrayPool, bufferLength: FileDownloaderSharedArrayPool.BufferLength); CurrentSegmentFileDownloader = segmentFileDownloader; await segmentFileDownloader.DownloadFileAsync(); // 下载完成逻辑 progress.Stop(); downloadFileInfo.DownloadSpeed = ""; downloadFileInfo.DownloadProcess = "完成"; downloadFileInfo.IsFinished = true; _ = DownloadFileListManager.WriteDownloadedFileListToFileAsync(DownloadFileInfoList.ToList()); // 后续优化多任务下载的时候的回收 _ = Task.Delay(TimeSpan.FromSeconds(3)) .ContinueWith(_ => ((FileDownloaderSharedArrayPool)SharedArrayPool).Clean()); }
private static async Task DownloadFileAsync(DownloadOption option) { var output = option.Output ?? ""; output = Path.GetFullPath(output); Console.WriteLine($"Download url = {option.Url}"); Console.WriteLine($"Output = {output}"); try { var loggerFactory = LoggerFactory.Create(builder => { }); var logger = loggerFactory.CreateLogger <SegmentFileDownloader>(); var progress = new Progress <DownloadProgress>(); var obj = new object(); DownloadProgress downloadProgress = null; progress.ProgressChanged += (sender, p) => { lock (obj) { downloadProgress = p; } }; bool finished = false; long lastLength = 0; DateTime lastTime = DateTime.Now; _ = Task.Run(async() => { while (!finished) { lock (obj) { if (downloadProgress == null) { continue; } Console.Clear(); Console.WriteLine($"Download url = {option.Url}"); Console.WriteLine($"Output = {output}"); Console.WriteLine( $"Process {downloadProgress.DownloadedLength * 100.0 / downloadProgress.FileLength:0.00}"); Console.WriteLine($"{downloadProgress.DownloadedLength}/{downloadProgress.FileLength}"); Console.WriteLine(); Console.WriteLine($"{(downloadProgress.DownloadedLength - lastLength) * 1000.0 / (DateTime.Now - lastTime).TotalMilliseconds / 1024 / 1024:0.00} MB/s"); lastLength = downloadProgress.DownloadedLength; lastTime = DateTime.Now; foreach (var downloadSegment in downloadProgress.GetCurrentDownloadSegmentList()) { Console.WriteLine(downloadSegment); } } await Task.Delay(500); } }); var file = new FileInfo(output); var url = option.Url; var segmentFileDownloader = new SegmentFileDownloader(url, file, logger, progress); await segmentFileDownloader.DownloadFile(); finished = true; } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e); Console.ResetColor(); } }