Exemple #1
0
        internal void Dequeue(object obj)
        {
            if (obj == null)
            {
                return;
            }
            var items = (obj as ObservableCollection <object>).Cast <DownloaderObjectModel>().ToArray();

            foreach (var item in items)
            {
                item.Dequeue();
            }
            QueueProcessor.Remove(items);
        }
Exemple #2
0
        private void Redownload(object obj)
        {
            var items        = (obj as ObservableCollection <object>).Cast <DownloaderObjectModel>().ToArray();
            var tasks        = new List <Task>();
            var forceEnqueue = false;
            var total        = items.Count();

            if (total > Settings.Default.MaxParallelDownloads)
            {
                forceEnqueue = true;
            }

            foreach (var item in items)
            {
                if (File.Exists(item.Destination))
                {
                    try
                    {
                        File.Delete(item.Destination);
                    }
                    catch (IOException)
                    {
                        continue;
                    }
                }
                if (forceEnqueue)
                {
                    item.Enqueue();
                    QueueProcessor.Add(item);
                }
                else
                {
                    item.Dequeue();
                    QueueProcessor.Remove(item);
                    tasks.Add(item.StartAsync());
                }
            }

            if (forceEnqueue)
            {
                Task.Run(async() => await QueueProcessor.StartAsync());
            }
            else
            {
                Task.Run(async() => await Task.WhenAll(tasks.ToArray()));
            }
        }
Exemple #3
0
        internal void Start(object obj)
        {
            var items        = (obj as ObservableCollection <object>).Cast <DownloaderObjectModel>().ToArray();
            var tasks        = new List <Task>();
            var forceEnqueue = false;
            var total        = items.Count();

            if (total > Settings.Default.MaxParallelDownloads)
            {
                forceEnqueue = true;
            }

            foreach (var item in items)
            {
                if (item.IsBeingDownloaded)
                {
                    continue;
                }
                if (forceEnqueue)
                {
                    item.Enqueue();
                    QueueProcessor.Add(item);
                }
                else
                {
                    item.Dequeue();
                    QueueProcessor.Remove(item);
                    tasks.Add(item.StartAsync());
                }
            }

            if (forceEnqueue)
            {
                Task.Run(async() => await QueueProcessor.StartAsync());
            }
            else
            {
                Task.Run(async() => await Task.WhenAll(tasks.ToArray()));
            }
        }
Exemple #4
0
        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();
        }