public void StartDownload(PreparedDownload download) { lock (active) { StartDownloadUnsafe(download); } }
private void StartDownloadUnsafe(PreparedDownload download) { if (!registered.Contains(download)) { download.StateChanged += dl_StateChanged; registered.Add(download); DownloadsAdded.RaiseEvent(this, new[] { download }); } Directory.CreateDirectory(Path.GetDirectoryName(download.FilePath)); active.Add(download); download.State = DownloadState.Active; download.Item.Module.StartDownload(download); }
private void RemoveActive(PreparedDownload download) { lock (active) { PreparedDownload dl; while (active.Contains(download)) { if (active.TryTake(out dl)) { break; } } } ProcessQueue(); }
private void dl_StateChanged(object sender, ValueChangedEventArgs <DownloadState> e) { PreparedDownload download = (PreparedDownload)sender; switch (e.NewValue) { case DownloadState.Active: break; case DownloadState.Completed: RemoveActive(download); completed.Add(download); break; case DownloadState.Failed: RemoveActive(download); stopped.Add(download); break; case DownloadState.Processing: break; case DownloadState.Queued: lock (queued) { if (!queued.Contains(download)) { queued.Enqueue(download); } } break; case DownloadState.Stopped: RemoveActive(download); stopped.Add(download); break; } }
private void ProcessQueue() { //Dont let anything touch the active downloads when we are processing the queue lock (active) { lock (queued) { if (!queued.IsEmpty) { if (active.Count < maxConcurrentDownloads) { PreparedDownload dl = null; while (!queued.IsEmpty && !queued.TryDequeue(out dl)) { } if (dl != null) { StartDownloadUnsafe(dl); } } } } } }
public abstract void StopDownload(PreparedDownload download);
public void StopDownload(PreparedDownload download) { download.Item.Module.StopDownload(download); download.State = DownloadState.Stopped; }