private void AddObjects(params DownloaderObjectModel[] objects) { Monitor.Enter(_lockDownloadItemsList); int total = objects.Count(); try { Application.Current.Dispatcher.Invoke(() => { for (int i = 0; i < total; i++) { if (objects[i] == null) { continue; } DownloadItemsList.Add(objects[i]); if (objects[i].IsQueued) { this.QueueProcessor.Add(objects[i]); } } }); } catch { } finally { Monitor.Exit(_lockDownloadItemsList); } }
private void CollectionView_CurrentChanged(object sender, EventArgs e) { var items = DownloadItemsList.ToArray(); int finished = 0; int queued = 0; int errored = 0; int ready = 0; int verifying = 0; int paused = 0; int downloading = 0; int total = items.Count(); foreach (var item in items) { switch (item.Status) { case DownloadStatus.Downloading: downloading++; break; case DownloadStatus.Error: errored++; break; case DownloadStatus.Finished: finished++; break; case DownloadStatus.Paused: paused++; if (item.IsQueued) { queued++; } break; case DownloadStatus.Queued: queued++; break; case DownloadStatus.Ready: ready++; break; case DownloadStatus.Verifying: verifying++; break; } } this.Count = total; this.DownloadingCount = downloading; this.ErroredCount = errored; this.FinishedCount = finished; this.PausedCount = paused; this.QueuedCount = queued; this.ReadyCount = ready; this.VerifyingCount = verifying; RaisePropertyChanged(nameof(this.Count)); RaisePropertyChanged(nameof(this.DownloadingCount)); RaisePropertyChanged(nameof(this.ErroredCount)); RaisePropertyChanged(nameof(this.FinishedCount)); RaisePropertyChanged(nameof(this.PausedCount)); RaisePropertyChanged(nameof(this.QueuedCount)); RaisePropertyChanged(nameof(this.ReadyCount)); RaisePropertyChanged(nameof(this.VerifyingCount)); RaisePropertyChanged(nameof(this.IsDownloading)); if (!this.IsBackgroundWorking) { if (downloading > 0) { this.Status = downloading + " item(s) downloading"; } else { if (_semaphoreUpdatingList.CurrentCount > 0) { this.Status = "Ready"; } } RaisePropertyChanged(nameof(this.Status)); } }
private async Task RemoveObjectsAsync(bool delete, params DownloaderObjectModel[] objects) { await _semaphoreUpdatingList.WaitAsync(); var dequeueThese = new List <IQueueable>(); var itemsProcessed = new List <DownloaderObjectModel>(); var total = objects.Count(); _ctsUpdatingList = new CancellationTokenSource(); var ct = _ctsUpdatingList.Token; RaisePropertyChanged(nameof(this.IsBackgroundWorking)); int progress; string primaryStatus = "Removing "; if (delete) { primaryStatus = "Deleting "; } for (int i = 0; i < total; i++) { progress = (int)((double)(i + 1) / total * 100); this.Status = primaryStatus + (i + 1) + " of " + total + ": " + objects[i].Name; this.Progress = progress; RaisePropertyChanged(nameof(this.Status)); RaisePropertyChanged(nameof(this.Progress)); if (objects[i] == null) { continue; } if (objects[i].IsQueued) { objects[i].Dequeue(); dequeueThese.Add(objects[i]); } if (objects[i].IsBeingDownloaded) { await objects[i].CancelAsync(); } else { // delete all UNFINISHED downloads forcefully if (objects[i].Status != DownloadStatus.Finished || delete) { try { if (objects[i].Status == DownloadStatus.Finished) { FileSystem.DeleteFile( objects[i].Destination, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin); } else { File.Delete(objects[i].TempDestination); } } catch { } } } itemsProcessed.Add(objects[i]); if (ct.IsCancellationRequested) { break; } } this.Status = "Delisting..."; RaisePropertyChanged(nameof(this.Status)); Application.Current.Dispatcher.Invoke(() => { Monitor.Enter(_lockDownloadItemsList); for (int i = 0; i < itemsProcessed.Count(); i++) { DownloadItemsList.Remove(itemsProcessed[i]); } Monitor.Exit(_lockDownloadItemsList); }); if (dequeueThese.Count > 0) { this.Status = "Refreshing queue..."; RaisePropertyChanged(nameof(this.Status)); QueueProcessor.Remove(dequeueThese.ToArray()); } _ctsUpdatingList = null; this.Status = "Ready"; this.Progress = 0; RaisePropertyChanged(nameof(this.Status)); RaisePropertyChanged(nameof(this.Progress)); RaisePropertyChanged(nameof(this.IsBackgroundWorking)); _semaphoreUpdatingList.Release(); }