public void SetDegreeOfParallelism(int degreeOfParallelism) { try { DegreeOfParallelism = degreeOfParallelism; } catch (Exception ex) { OnDownloaderException?.Invoke(ex); } }
public void AddFileToDownloadingQueue(string fileId, string url, string pathToSave) { try { DownloadQueue.Enqueue(fileId); var download = new Download(fileId, url, pathToSave, CreateDownload(url, pathToSave, fileId)); if (!AllDownloads.TryAdd(fileId, download)) { throw new Exception($"An exception while adding {fileId}"); } Task.Factory.StartNew(() => OnFileProgress?.Invoke(fileId, 0, 0)).Wait(); } catch (Exception ex) { OnDownloaderException?.Invoke(ex); } ManageDownloads(); }
private async Task ManageDownloads() { try { if (DownloadingNow.Count < DegreeOfParallelism && DownloadQueue.Count > 0) { for (int i = 0; i < degreeOfParallelism - DownloadingNow.Count; i++) { string download; if (DownloadQueue.TryDequeue(out download)) { DownloadingNow.Add(download); } } } if (DownloadingNow.Count > 0) { var downloads = DownloadingNow.ToArray(); foreach (string id in downloads) { var download = GetFromAllDownloads(id); if (download.DownloadTask.Status == TaskStatus.RanToCompletion || download.DownloadTask.Status == TaskStatus.Faulted) { RemoveFromDownloadingNow(id); } if (download.DownloadTask.Status == TaskStatus.WaitingForActivation) { RunDownloadTask(download.DownloadTask); } } } } catch (Exception ex) { OnDownloaderException?.Invoke(ex); } }