Пример #1
0
 internal void StartQueue(object obj)
 {
     Task.Run(async() =>
     {
         await QueueProcessor.StartAsync();
         this.Status = "Ready";
         RaisePropertyChanged(nameof(this.Status));
     });
 }
Пример #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()));
            }
        }
Пример #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()));
            }
        }
Пример #4
0
        private async Task AddItemsAsync(string destination, bool enqueue, bool start = false, params string[] urls)
        {
            await _semaphoreUpdatingList.WaitAsync();

            _ctsUpdatingList = new CancellationTokenSource();
            var ct = _ctsUpdatingList.Token;

            RaisePropertyChanged(nameof(this.IsBackgroundWorking));

            int           total = urls.Count();
            int           maxParallelDownloads = Settings.Default.MaxParallelDownloads;
            var           items                = new DownloaderObjectModel[urls.Count()];
            var           tasks                = new List <Task>();
            var           forceEnqueue         = false;
            var           existingUrls         = (from di in DownloadItemsList select di.Url).ToArray();
            var           existingDestinations = (from di in DownloadItemsList select di.Destination).ToArray();
            List <string> skipping             = new List <string>();
            var           wasCanceled          = false;

            if (start && !enqueue && urls.Count() > Settings.Default.MaxParallelDownloads)
            {
                forceEnqueue = true;
            }

            for (int i = 0; i < total; i++)
            {
                int progress = (int)((double)(i + 1) / total * 100);
                this.Progress = progress;
                this.Status   = "Creating download " + (i + 1) + " of " + total + ": " + urls[i];
                RaisePropertyChanged(nameof(this.Status));
                RaisePropertyChanged(nameof(this.Progress));

                if (existingUrls.Contains(urls[i]))
                {
                    skipping.Add(urls[i]);
                    continue;
                }
                var fileName = CommonFunctions.GetFreshFilename(destination + Path.GetFileName(urls[i]));
                if (existingDestinations.Contains(fileName))
                {
                    skipping.Add(urls[i]);
                    continue;
                }

                DownloaderObjectModel item;
                if (forceEnqueue || enqueue)
                {
                    item = new DownloaderObjectModel(
                        ref _client,
                        urls[i],
                        fileName,
                        enqueue: true,
                        Download_Created,
                        Download_Verifying,
                        Download_Verified,
                        Download_Started,
                        Download_Stopped,
                        Download_Enqueued,
                        Download_Dequeued,
                        Download_Finished,
                        Download_PropertyChanged,
                        ProgressReporter,
                        ref _requestThrottler);
                }
                else
                {
                    item = new DownloaderObjectModel(
                        ref _client,
                        urls[i],
                        fileName,
                        enqueue: false,
                        Download_Created,
                        Download_Verifying,
                        Download_Verified,
                        Download_Started,
                        Download_Stopped,
                        Download_Enqueued,
                        Download_Dequeued,
                        Download_Finished,
                        Download_PropertyChanged,
                        ProgressReporter,
                        ref _requestThrottler);
                    if (start)
                    {
                        tasks.Add(item.StartAsync());
                    }
                }
                items[i] = item;

                if (ct.IsCancellationRequested)
                {
                    wasCanceled = true;
                    break;
                }
            }

            if (!wasCanceled)
            {
                this.Status = "Listing...";
                RaisePropertyChanged(nameof(this.Status));

                AddObjects(items);
            }

            _ctsUpdatingList = null;

            this.Progress = 0;
            this.Status   = "Ready";
            RaisePropertyChanged(nameof(this.Status));
            RaisePropertyChanged(nameof(this.Progress));
            RaisePropertyChanged(nameof(this.IsBackgroundWorking));

            _semaphoreUpdatingList.Release();

            RefreshCollection();

            if (!wasCanceled)
            {
                if (skipping.Count > 0)
                {
                    _showUrls(
                        skipping, "Duplicate Entries",
                        "The following URLs were not added because they are already in the list:");
                }

                if ((enqueue && start) || forceEnqueue)
                {
                    await QueueProcessor.StartAsync();
                }
                else
                {
                    await Task.WhenAll(tasks);
                }
            }
        }