Exemplo n.º 1
0
        public async void Init()
        {
            var downloadedFileInfoList = await DownloadFileListManager.ReadDownloadedFileListAsync();

            if (downloadedFileInfoList != null)
            {
                DownloadFileInfoList.AddRange(downloadedFileInfoList);
            }
        }
Exemplo n.º 2
0
        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());
        }