private void CancelAll()
 {
     Cts.Cancel();
     Cts.Dispose();
     Cts = new CancellationTokenSource();
     ActiveDownloads.Clear();
 }
        private async Task <Uri> HandleDownload(DownloadOperation download, bool alreadyStarted)
        {
            var bindableDownload = new BindableDownloadOperation(download);

            try {
                ActiveDownloads.Add(bindableDownload);
                var callback = new Progress <DownloadOperation>(OnDownloadStatusUpdate);
                IAsyncOperationWithProgress <DownloadOperation, DownloadOperation> op = null;
                if (alreadyStarted)
                {
                    var d = download.AttachAsync();
                    op = download.AttachAsync();
                }
                else
                {
                    op = download.StartAsync();
                }
                op.Progress = (a, p) => {
                    OnBindableStatusUpdate(bindableDownload);
                };
                // awaits completion of download
                DownloadOperation completedDownload = await op.AsTask(Cts.Token, callback);

                return(FileUtils.UriFor(completedDownload.ResultFile));
            } finally {
                // download complete
                ActiveDownloads.Remove(bindableDownload);
            }
        }
        /// <summary>
        ///     Use as callback of every downloads progress
        /// </summary>
        /// <param name="download">
        ///     The download that has just progressed
        /// </param>
        private void DownloadProgress(DownloadOperation download)
        {
            // Thread safety comes first!
            dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
            {
                // Get the associated song BackgroundDownload
                var songDownload =
                    ActiveDownloads.FirstOrDefault(
                        p => ((DownloadOperation)p.Download.DownloadOperation).Guid == download.Guid);

                if (songDownload == null)
                {
                    return;
                }

                songDownload.Download.Status = download.Progress.Status.ToString();

                if (download.Progress.TotalBytesToReceive > 0)
                {
                    songDownload.Download.BytesToReceive = download.Progress.TotalBytesToReceive;
                    songDownload.Download.BytesReceived  = download.Progress.BytesReceived;
                }
                else
                {
                    songDownload.Download.Status = "Waiting";
                }
            });
        }
        /// <summary>
        ///     Hanbdles a single BackgroundDownload for a song.
        /// </summary>
        /// <param name="song">
        ///     The song to be downloaded
        /// </param>
        /// <param name="download">
        ///     The download operation
        /// </param>
        /// <param name="start">
        ///     Either the download is started or just handled
        /// </param>
        private async void HandleDownload(Song song, DownloadOperation download, bool start)
        {
            if (song == null || download == null)
            {
                return;
            }

            song.Download = new BackgroundDownload(download);
            ActiveDownloads.Add(song);

            try
            {
                var progressCallback = new Progress <DownloadOperation>(DownloadProgress);
                if (start)
                {
                    // Start the BackgroundDownload and attach a progress handler.
                    await download.StartAsync().AsTask(song.Download.CancellationTokenSrc.Token, progressCallback);
                }
                else
                {
                    // The BackgroundDownload was already running when the application started, re-attach the progress handler.
                    await download.AttachAsync().AsTask(song.Download.CancellationTokenSrc.Token, progressCallback);
                }

                // Download Completed
                var response = download.GetResponseInformation();

                // Make sure it is success
                if (response.StatusCode < 400)
                {
                    await DownloadFinishedForAsync(song);
                }
                else
                {
                    song.SongState = SongState.None;
                    sqlService.UpdateItem(song);
                    download.ResultFile.DeleteAsync();
                }
            }
            catch
            {
                song.SongState = SongState.None;
                sqlService.UpdateItem(song);
                download.ResultFile.DeleteAsync();
            }
            finally
            {
                ActiveDownloads.Remove(song);
            }
        }
Beispiel #5
0
    public void Update()
    {
        foreach (var d in ActiveDownloads)
        {
            d.AttemptResolve();
            if (d.Resolved)
            {
                DownloadsToClear.Add(d);
            }
        }

        foreach (var d in DownloadsToClear)
        {
            ActiveDownloads.Remove(d);
            ImageDownloads.Remove(d.URL);
        }
    }
Beispiel #6
0
    public Texture2D GetTexture(string url)
    {
        if (string.IsNullOrEmpty(url))
        {
            return(null);
        }
        if (Images.ContainsKey(url))
        {
            return(Images[url]);
        }

        if (ImageDownloads.ContainsKey(url))
        {
            // waiting for download
            return(null);
        }

        var download = new ImageDownload(this, url);

        ImageDownloads.Add(url, download);
        ActiveDownloads.Add(download);
        return(null);
    }